C++ Arrays | Find output programs | Set 5

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

Program 1:

#include <iostream>
using namespace std;

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

    cout << (*STR + 2)[2];

    return 0;
}

Output:

O

Explanation:

Here we create an array of pointers to store strings. Now, look at the cout statement,

cout<<(*STR+2)[2];

The above statement will print the element of the 4th index of 1st string because pointer STR pointing the 1st string that is "HELLO" so the above statement will print 'O'.

Program 2:

#include <iostream>
using namespace std;

int main()
{
    char STR[5][8] = { "HELLO", "HIII", "RAM", "SHYAM", "MOHAN" };

    cout << STR[2] + 1;

    return 0;
}

Output:

AM

Explanation:

Here, we created a program two-dimensional array for strings. Now look the cout statement,

cout<<STR[2]+1;

In the above statement, STR[2] is pointing to the 'R' in the string  "RAM" and we move pointer one position ahead then it will print "AM" on the console screen.






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.