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;
}
Answer Program 1:
Output:
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:
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:
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