Output of C++ programs | Set 39 (Pointers)

Prerequisite: Pointers in C/C++

QUE.1 What would be printed from the following C++ program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
    float x = 5.999;
    float* y, *z;
    y = &x;
    z = y;
    cout << x << ", " << *(&x) << ", " << *y << ", " << *z << "\n";
    return 0;
}

chevron_right


a) 5.999, 5.999, 5.999, 5.999
b) 5.999, 5.9, 5.000, 5.900
c) Address of the elements
d) compilation error



 Answer: a

Explanation: The reason for this is x gives the value stored in the variable x. *(&x) gives the data value stored in the address &x i.e., the data value of x. Since y points to x (..y=&x), *y gives the value of x. And because z has the same address as that of y, *z also gives the value of x i.e., 5.999

QUE.2 What would be printed from the following C++ program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
int main()
{
    int track[] = { 10, 20, 30, 40 }, *striker;
  
    striker = track;
    track[1] += 30;
    cout << "Striker>" << *striker << " ";
    *striker -= 10;
    striker++;
    cout << "Next@" << *striker << " ";
    striker += 2;
    cout << "Last@" << *striker << " ";
    cout << "Reset To" << track[0] << " ";
  
    return 0;
}

chevron_right


a) 10, 20, 30, 40
b) Striker>10 Next@50 Last@40 Reset To0
c) Striker>10 Next@40 Last@50 Reset To0
d) Striker> Next@ Last@ Reset To

 Answer: b

Explanation: The array track contains 4 elements {10, 20, 30, 40} and the pointer striker holds the base address of the array track i.e, address of track[0].
1) *striker holds the data value of track[0] i.e, 10. Decrement in *striker by 10 makes the track[0]=0.
2) Incrementing pointer striker gives the next location of track i.e., 1. Now *striker gives the data value of track[1].
3) Again by incrementing by 2, striker reaches to the 4 address of the array track i.e, track[4].
4) At last print the value at track[0], which is 0 (see step 1)

QUE.3 What would be printed from the following C++ program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
int main()
{
    int a = 32, *ptr = &a;
  
    char ch = 'A', &cho = ch;
    cho += a;
    *ptr += ch;
    cout << a << ", " << ch << endl;
    return 0;
}

chevron_right


OPTION
a) 97, A
b) 128, A
c) 97, a
d) 129, a

Answer: d

Explanation: ptr is a pointer which holds the address of a while *ptr returns the data value of a. Cho is a reference variable which hold the reference of ch. Now, incrementing the value of cho by 32 (ASCII value), reflect to cho and ch makes it equal to “a” (alphabet). In last step, data value of *ptr incremented by ch i. e., “a” gives a = 129.

QUE.4 What is the output of the following C++ program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
int main()
{
    const int i = 20;
    const int* const ptr = &i;
    (*ptr)++;
    int j = 15;
    ptr = &j;
    return 0;
}

chevron_right


a) Address of the elements
b) run time error
c) Compilation error
d) none of this

Answer:c

Explanation: ptr is a constant pointer to constant integer, which means neither the pointer nor its contents will be modified, thus lines 6 and 8 are invalid as they are trying to modify the contents and pointer respectively.


QUE.5 What is the output of the following C++ program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
#include <stdio.h>
int main()
{
    char* str[] = { "AAAAA", "BBBBB", "CCCCC", "DDDDD" };
    char** sptr[] = { str + 3, str + 2, str + 1, str };
    char*** pp;
  
    pp = sptr;
    ++pp;
    printf("%s", **++pp + 2);
    return 0;
}

chevron_right


a) BBBBB
b) CCCCC
c) BBB
d) Error

Answer: c

Explanation: *str is a array pointer of string, **sptr is array pointer(double pointer) that is pointing to str strings in reverse order. ***pp also a pointer that is pointing sptr base address. ++pp will point to 1st index of sptr that contain str+2 (“CCCCC”). in printf(“%s”, **++pp+2); ++pp will point to str+1, and **++pp, value stored @ str+1 (“BBBBB). and (**++pp)+2 will point the 2nd index of “BBBBB”, hence BBB will be printed.

This article is contributed by Ajay Puri(ajay0007). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.