pointer to pointer issue



1
2
3
4
5
6
  int a,b;
  int *ptr;
  int**ptrptr;

  ptr =&a;
  ptrptr=&ptr;


Using no other objects besides those already declared, how can
you alter ptrptr so that it points at a pointer to b without
directly touching ptr ?
Last edited on
closed account (1vRz3TCk)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    int a = 5, b = 10;
    int *ptr = &a;
    int **ptrptr = &ptr;

    std::cout << (*ptr) << "\n";
    //
    // dereference the pointer-to-pointer-to-int once to get
    // the pointer-to-int it is pointing to and asign the address of
    // b to that pointer.
    *ptrptr = &b;

    std::cout << (*ptr) << "\n";
}
5
10
Press any key to continue . . .
Last edited on
thanks
Topic archived. No new replies allowed.