Fun with Pointers 2

Consider the following C++ code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;

int main() {

    int *p, *q;

    int x = 1;

    p = &x;

    *p = 2;

    q = p;

    *q = 3;

    printf("%d\n", *p + *q + x);

    return 0;
}

What will the program outputs?

8 6 9 7

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Hasmik Garyaka
Sep 25, 2017

p and q point to x and is 3 in that memory address.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...