Q:

C++ Arrays | Find output programs | Set 4

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Arrays (set 4).

Program 1:

#include <iostream>
using namespace std;

int main()
{
    char A[5] = { 'H', 'E', 'L', 'L', 'O' };
    char ch;

    ch = (A + 2)[2];

    cout << ch;

    return 0;
}

Program 2:

#include <iostream>
using namespace std;

int main()
{
    char A[2][3] = { 'H', 'E', 'L', 'L', 'O' };

    cout << A[1][0] << " " << A[1][2] << " ";

    return 0;
}

Program 3:

#include <iostream>
using namespace std;

int main()
{
    char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };

    cout << STR[1];

    return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

O

Explanation:

Here, we created a character array initialized with some character values. Now look the below statement,

ch = (A+2)[2];

In the above statement, we are accessing the element of index 4. That is 'O'. Then finally 'O' will be printed on the console screen.

Answer Program 2:

Output:

L

Explanation:

Here, we created a two-dimensional character array initialized with some character values.

Here the values of array will be like this,

A[0][0] = 'H';
A[0][1] = 'E';
A[0][2] = 'L';
A[1][0] = 'L';
A[1][1] = 'O';
A[1][2] = '\0';

So,
A[1][0] = 'L' and A[1][2] = '\0';

Then, the L will print, but '\0' character cannot print on the console screen.

Answer Program 3:

Output:

main.cpp:6:21: warning: character constant too long for its type
     char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
                     ^~~~~~~
main.cpp:6:30: warning: multi-character character constant [-Wmultichar]
     char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
                              ^~~~~~
main.cpp:6:38: warning: multi-character character constant [-Wmultichar]
     char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
                                      ^~~~~
main.cpp:6:45: warning: character constant too long for its type
     char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
                                             ^~~~~~~
main.cpp:6:54: warning: character constant too long for its type
     char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
                                                      ^~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:6:62: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]
     char* STR[] = { 'HELLO', 'HIII', 'RAM', 'SHYAM', 'MOHAN' };
                                                              ^
main.cpp:6:62: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]
main.cpp:6:62: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]
main.cpp:6:62: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]
main.cpp:6:62: error: invalid conversion from ‘int’ to ‘char*’ [-fpermissive]

Explanation:

The above code will generate an error (invalid conversion from 'int' to 'char*' [-fpermissive]) because we enclosed string values within a single quote that is not the correct way. We can enclose a string within double-quotes.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Arrays | Find output programs | Set 5... >>
<< C++ Arrays | Find output programs | Set 3...