Home > OS >  Why is that for a pointer *p, p[0] is the address stored at p and p[1] is the address of p itself?
Why is that for a pointer *p, p[0] is the address stored at p and p[1] is the address of p itself?

Time:04-20

The code

int n = 25;  
int *p = &n;  
printf("%x\n %d\n %x\n", p, p[0], p[1]);

returns:

\<adress-of-p  
25  
\<adress-of-p>  

Of course I would never do this but in K&R states that

"if pa is a pointer, expressions may use it with a subscript; pa[i] is identical to *(pa i).

so I was curious.

CodePudding user response:

This statement

printf("%x\n %d\n %x\n", p, p[0], p[1]);

invokes undefined behavior by two reasons.

The first one is that to output a pointer you should use a correct conversion specifier. The second one is that you may not dereference a pointer like this p[1] that does not point to a valid object.

Instead you could write for example

printf("%p\n %d\n %p\n", ( void * )p, p[0], ( void * )( p   1 ) );

CodePudding user response:

When you evaluate p[1] in your code, you are invoking undefined behavior so your program can do anything.

It is undefined behavior because p points at n which is just a single integer, not an array of integers. So p[0] is n, but p[1] is undefined. Basically this is an array overflow bug.

CodePudding user response:

Your program has undefined behaviour, because it dereferences a pointer that doesn't point to anything.

The particular symptoms you have experienced are consistent with reading the next variable in the function

CodePudding user response:

The p[n] syntax is syntactic sugar for *(p n).

So p[0] is *(p 0), which is *p, and since p points to n, *p is the value of n, which is 25.

Now, p[1] is *(p 1). So in theory what you would get is the next integer in memory following n. In this particular case, the next thing in memory happened to be p itself, which is the address of n. (Note that this isn't guaranteed, your compiler just chose to arrange things that way.)

  • Related