Originally Posted by
ddutch
A pointer represents a memory address/location, the value of a pointer is a memory-address. So for example an ampersand in front of "int i" also represents a pointer:
int i;
&i is thus a pointer but as a pointer it represents a pointer-constant since you can not change it's address (only the integer value i can be changed). Obviously you can assign any integer
value to i but you can NOT change the address of i. The following is NOT possible: &i = someOtherValue;
Pointer variables can point to different addresses like for example:
int *iPtr, i, j;
iPtr = &i; // iPtr points to the memory location of the integer variable i
iPtr = &j; // Now iPtr points to the memory location of the integer variable j
If you only declare a variable without assigning it with a value, like for example "int i;" the value of i is undefined. Although at that stage, i is not yet provided with a value, it does have a value (which can be any integer value), therefore it's value is undefined.
The same holds for pointers, like for example you declare "int *iPtr;" It's value can be anything, in other words, it can point to any memory location, So if you assign a value to such an un-initialized pointer, it is very well possible that you try to store a value in a memory location to which your program has no access. In that case your operating system will signal your program that something bad has happened.
The statement iPtr = NULL explicitly means that iPtr is NOT pointing to any valid/allocated memory and thus de-referencing it is illegal.
int *iPtr is a pointer variable which like any variable has a specific address.
As mentioned above: "A pointer represents a memory address/location" .... so it is also possible to declare a pointer which points to the memory address of a pointer variable, like in:
int i, j;
int *iPtr, *jPtr; // pointers to integers
int **iPptr; // pointer to a pointer to integer (you can make the list as long as you want, for example int ***complexPtr is a pointer to a pointer to a pointer to an integer)
iPtr = &i; // iPtr now gets as value the address of i, in other words iPtr points to the memory location of integer variable i
*iPtr = 5; // the memory location to which iPtr points now gets the value 5 and since iPtr points to the memory location of i, this results in assigning the value 5 to i --> i ==5
iPptr = &iPtr; // iPptr now gets as value the address of iPtr, in other words iPptr points to the memory location of pointer variable iPtr
jPtr = *iPptr; // jPtr now gets as value the value of iPptr, in other words jPtr==iPtr and they now both point to the same address (which is integer variable i)
// now i == *iPtr == *jPtr == **iPptr
........, I'm not sure whether this makes it a bit clearer for you