I dont really understand this program...
Can somebody help me to understand this program..
1 2 3 4 5
int x;
int * p1 = &x; // non-const pointer to non-const int
constint * p2 = &x; // non-const pointer to const int
int * const p3 = &x; // const pointer to non-const int
constint * const p4 = &x; // const pointer to const int
A pointer is simply a variable that holds the memory location of a variable.
Line 2 is making a pointer that'll hold and "int" type of variable. The "&x" is there because the "&" operator (when used like that) will give the memory address of the variable that follows.
Line 3 is making a pointer which is holding the memory address of a the variable. However, declaring the const there means this pointer can't be used to actually change the value held in that memory address.
Line 4 is making a pointer which is constant. Meaning the memory address the pointer is holding can't be changed, but it does nothing to prevent the actual data held in that memory address to be altered.
Line 5 is a combo of 3 and 4. The pointer can't change the memory address it's holding AND the pointer can't be used to change the value within that memory address.