Home > front end >  pointer of pointer (**) print question, how does it work?
pointer of pointer (**) print question, how does it work?

Time:08-01

Continuing my C studies, I started trying to do all sorts of things with pointers. And below is the code I worked on

int main()
{
    int array[] = { 3,2,6776,4 };
    int *x;
    int** pointer = &x;
    
    doIT((int *)array, 4, pointer);
    printf("\n Value  %d",**pointer);
    printf("\n Addrees 1  %p",*&pointer);
    printf("\n address 2  %p", &pointer);
    
}

void doIT(int *p, int sizes, int** pointer) {
    for (int i = 0; i < sizes; i  )
    {
        if (i == 2)
        {
         *pointer = p;
        }
        p  ;
    }
}

Console The address, memory

I tried to play with pointers and see what I would get in their prints so that I could understand how the pointers really work, and I didn't understand something, how the first address got the first value of Pointer, after all I used *&, that is, I accessed the value of the first pointer and from there I took the other's address,

And when printing the second address, why don't I print the main address of the pointer? (0x0097f804)?

I would really appreciate some help or even some kind of drawing in order to really understand the issue of pointers Thanks a lot guys

CodePudding user response:

Whenever you have problems understanding pointers, or problems visualizing them, bring out some paper and a pencil and draw it. Boxes for data, variables, etc., and arrows for the pointers.

If we do it in a little more primitive way, after the call

doIT((int *)array, 4, pointer);

you will have something that looks like this:

                           ---------- 
                          | array[0] |
                           ---------- 
                          | array[1] |
 ---------       ---       ---------- 
| pointer | --> | x | --> | array[2] |
 ---------       ---       ---------- 
                          | array[3] |
                           ---------- 

When you print *&pointer then that's the same as pointer.

When you print &pointer then that's the location of the pointer variable. I.e. it's a pointer to the variable pointer and will have the type int ***.

CodePudding user response:

The pointer pointer stores the address of the pointer x

int *x;
int** pointer = &x;

So these two calls of printf will output the same value

printf( "pointer = %p\n", ( void * )pointer );
printf( "&x = %p\n", ( void * )&p );

The expression &pointer yields the address of the variable pointer. That is the expression points to the pointer pointer. Dereferencing the expression *( &pointer ) you will get the original variable pointer. So the expression *&pointer is the same as the expression pointer.

To make it more clear consider the following simple program.

#include <stdio.h>

int main( void )
{
    int x = 10;

    printf( "x = %d\n", x );
    printf( "*&x = %d\n", *&x );
}

Its output is

x = 10
*&x = 10

The difference between this demonstration program and your code is that the variable x has the type int while the variable pointer has the type int **. But in the both cases calls of printf output the values stored in these variables.

  • Related