Here is an example which will, I hope, make it more clear what's going on.
#include <stdio.h>
int main()
{
int var = 20;
int *ptr = &var;
int *ptr2 = ptr;
printf("the value of the variable var is %d\n", var);
printf("the address of the variable var is %p\n\n", &var);
printf("the value of the pointer ptr is %p\n", ptr);
printf("the value pointed to by ptr is %d\n", *ptr);
printf("or, stated another way, it's %d\n\n", ptr[0]);
printf("the value of the pointer ptr2 is %p\n", ptr2);
printf("the value pointed to by ptr2 is %d\n", *ptr2);
}
On my machine this prints:
the value of the variable var is 20
the address of the variable var is 0x7ffeed56992c
the value of the pointer ptr is 0x7ffeed56992c
the value pointed to by ptr is 20
or, stated another way, it's 20
the value of the pointer ptr2 is 0x7ffeed56992c
the value pointed to by ptr2 is 20
Since ptr
and ptr2
have the same type (int *
), and since they hold the same pointer value (since we said ptr2 = ptr
), they behave the same.
And because of the "correspondence between arrays and pointers" in C, *ptr
is identical to ptr[0]
. Both expressions yield the value pointed to by ptr
.
If your intention with ptrptr
was to have it be a pointer to a pointer, here's an illustration of that wrinkle. Since ptrptr
is a two-level pointer, its pointed-to value is actually another pointer, so you have to be really careful thinking about it.
int **ptrptr = &ptr;
printf("the value of the pointer ptrptr is %p\n", ptrptr);
printf("the value pointed to by ptrptr is %p\n", *ptrptr);
printf("and the value pointed to by that pointer is %d\n", **ptrptr);
This additionally prints:
the value of the pointer ptrptr is 0x7ffeed569920
the value pointed to by ptrptr is 0x7ffeed56992c
and the value pointed to by that pointer is 20