C++ Arrays | Find output programs | Set 4

This section contains the C++ find output programs with their explanations on C++ Arrays (set 4).
Submitted by Nidhi, on June 09, 2020

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;
}

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.

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;
}

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.

Program 3:

#include <iostream>
using namespace std;

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

    cout << STR[1];

    return 0;
}

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.