Have a look at this code snippet:
int i = 10;
int *pi = &i;
int **ppi = π // first declaration
int *api = pi; // second declaration
printf("i's value is: %d\n",i);
printf("pi's value is: %d\n",*pi);
printf("ppi's value is: %d, at the address: %d\n",**ppi);
printf("api's value is: %d, at the address: %d\n",*api);
Output
$ ./test
i's value is: 10
pi's value is: 10
ppi's value is: 10, at the address: 2686760
api's value is: 10, at the address: 2686760
So which way is (perhaps) more preferrable in those 2 declarations of pointer to pointer, and is there any technical difference between those two?
Technically, you’ve only declared one pointer-to-pointer.
The second declaration is just a pointer which, when assigned, gets a copy of the first pointer
pi‘s address.Here’s how you can prove it; after your test code, add this:
The output will be:
When you change the value of
pi(what it points to), dereferencingppiwill reflect the changes, as it points topi, but becauseapiwas just made as a copy ofpibefore it changed, it will continue pointing toi.