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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.