The Student Room Group

basic C programming (pointers)

int main() {

int i = 5;
int *iPointer = &i;
int **ipp = &iPointer;
printf("%i", **ipp);
return 0;

}

why does this output 5? doesn't **ipp point to the ADDRESS of *iPointer (which points to the address of i rather than the value)?

any help is much appreciated
Original post by shartos maximus
int main() {

int i = 5;
int *iPointer = &i;
int **ipp = &iPointer;
printf("%i", **ipp);
return 0;

}

why does this output 5? doesn't **ipp point to the ADDRESS of *iPointer (which points to the address of i rather than the value)?

any help is much appreciated


Actually iPointer (no stars) is pointing to the address of i, the variable **ipp has taken value of the address of iPointer of the address of i of the value of i.

This usually tricks most newbies with C and C++, the stars are important!
The more stars you have the deeper you are going basically:


int i = 5;
int *iPointer = &i;
int **ipp = &iPointer;
int ***yolo = &ipp;


here yolo is pointing to the address of ipp, *yolo is the value of ipp which is the address of iPointer, **yolo is the address of i and ***yolo is the value of i which is 5.

To be honest I am not a fan of pointers myself, but think of it this way for this instance:
The * operator is the opposite of the & operator, anywhere you see a * indicates how deep you want to infer of a provided address. It is sort of like having many variables per declaration, e.g. yolo, *yolo, **yolo and ***yolo are all variables.

I hope that makes some sense. If not, keep testing it out, read a guide on the net if you have to.
(edited 10 years ago)
yeah i played around with it and eventually understood it.. thanks for the clarification though

the yolo pointer example sealed the deal
Original post by shartos maximus
yeah i played around with it and eventually understood it.. thanks for the clarification though

the yolo pointer example sealed the deal


You're welcome

Quick Reply

Latest

Trending

Trending