skip to Main Content

I am self learning C++ and I found my self on the Pointers section of C++.

To my understanding the pointer allocates the value of a variable to a memory.

But I came across on this problem which the answer is 588.

And I cannot figure out how this number came up.

Can someone please explain me step by step how 588 came up ?

Thanks in advance.


#include <iostream>

int main() {
    int *p, *q, *t;

    // Allocate memory and initialize pointers and values
    p = new int;
    q = new int;
    t = new int;
    *p = 17;
    *q = 7;
    *t = 42;

    p = t; // Make pointer p point to the same location as pointer t
    t = q; // Make pointer t point to the same location as pointer q

    *t = *p * *q;

    *q = *q + *t;

    std::cout << *t << std::endl;


    return 0;
}

3

Answers


  1. There is written in the comments of the program

    p = t; // Make pointer p point to the same location as pointer t
    t = q; // Make pointer t point to the same location as pointer q
    

    That is after these statements *p is equal to 42 and *t is equal to 7 the same way as *q is equal to 7 because now the both pointers t and q point to the same memory.

    As a result you have that this statement

    *t = *p * *q;
    

    is euivalent to

    *t = 42 * 7;
    

    That is the object pointed to by the pointers t and q now contains 294.

    This statement

    *q = *q + *t;
    

    that is the same as

    *t = *q + *t;
    

    because the both pointers t and q point to the same object and also is the same as

    *t = *t + *t;
    

    or

    *q = *q + *q;
    

    So 294 plust 294 yelds 588.

    Login or Signup to reply.
  2. You can easily get what is going on if you output all three pointers and theirs values after each change to them, or by using a debugger. This is what happens:

    *p = 17;
    *q = 7;
    *t = 42;
    
    // p points to an int with value 17
    // q points to an int with value 7
    // t points to an int with value 42
    
    p = t; // Make pointer p point to the same location as pointer t
    
    // p points to an int with value 42 (the same as t)
    // q points to an int with value 7
    // t points to an int with value 42 (the same as p)
        
    t = q; // Make pointer t point to the same location as pointer q
    
    // p points to an int with value 42 (now different than t)
    // q points to an int with value 7 (the same as t)
    // t points to an int with value 7 (the same as q)
    
    *t = *p * *q;   // *t = (*p) * (*q) = 42 * 7 = 294
    
    // p points to an int with value 42
    // q points to an int with value 294 (still the same as t)
    // t points to an int with value 294 (still the same as q)
    
    *q = *q + *t;   // *q = (*q) + (*t) = 294 + 294 = 588
    
    // p points to an int with value 42
    // q points to an int with value 588 (still the same as t)
    // t points to an int with value 588 (still the same as q)
    
    std::cout << *t << std::endl;   // prints 588 (the same as q)
    

    However note that something like p = t overwrites the address p was pointing to, therefore you cant free up the allocated memory anymore.

    Login or Signup to reply.
  3. When in doubt, you should draw it out! For example…

    int *p, *q, *t;
    

    You are declaring 3 pointers that don’t point anywhere yet, thus:

    p -> ?
    q -> ?
    t -> ?
    
    p = new int;
    q = new int;
    t = new int;
    

    You are allocating 3 integers (with indeterminate initial values), and making the 3 pointers point at them, thus:

    p -> [ ... ]
    q -> [ ... ]
    t -> [ ... ]
    
    *p = 17;
    *q = 7;
    *t = 42;
    

    You are dereferencing the pointers and setting the values of the integers they are pointing at, thus:

    p -> [ 17 ]
    q -> [ 7  ]
    t -> [ 42 ]
    
    p = t;
    

    You are making p point at the same integer that t is pointing at, thus:

    p -+   [ 17 ]
       |
    q -|-> [ 7  ]
       |
    t -+-> [ 42 ]
    

    t = q;

    You are making t point at the same integer that q is pointing at, thus:

    p -+     [ 17 ]
       |
    q -|-+-> [ 7  ]
       | |
    t -|-+
       |
       +---> [ 42 ]
    
    *t = *p * *q;
    

    You are multiplying the values of the integers that p and q are pointing at (42 * 7), and assigning the result (294) to the integer that t is pointing at, thus:

    p -+     [ 17 ]
       |
    q -|-+-> [ 294 ]
       | |
    t -|-+
       |
       +---> [ 42 ]
    
    *q = *q + *t;
    

    You are adding the value of the integer that q and t are both pointing at (294 + 294), and assigning the result (588) to the integer that q is pointing at, thus:

    p -+     [ 17 ]
       |
    q -|-+-> [ 588 ]
       | |
    t -|-+
       |
       +---> [ 42 ]
    
    std::cout << *t << std::endl;
    

    You are printing out the value of the integer that t is pointing at, thus:

    588

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search