pointers
are like any other data type
in C programming language. pointers
have names and values and when they born we can initialize them or not.
The special property in pointers
is their values. They are designated to hold/contain the address
of other data type, But that does not contradict the fact that they are also variables that lives in certain address in the memory/ram!
Consider the code snippet below:
int x = 10;
x is data type, his type is integer and its value is 10
int* p_x = &x;
p_x
is data type, his type is int*
(pointer to int
) and his value is the address of x (&x
)
int* p;
p
is data type, his type is int*
(pointer to int
) and his value is unknown (valid address in the system). this is very bad. it called dangling pointer
and rule number one, when you declare a pointer you initialize it immediately (NULL
is used often if you don't have a valid address for initializing in declaration time). please consider that compilers are smart and can initialize uninitialized pointer to null
. but never relay on this.
int** p_px = &p_x;
p_px
is data type, his type is int**
(pointer to int*
, OR pointer to int pointer
) and his value is the address of p_x (&p_x
), where p_x
is found in ram.
Let's see an Illustration of the above variables in RAM:
*---------------------------------------------------------*
* Address | Value | var Name *
*---------------------------------------------------------*
* 0x7fff4ee2095c | 10 | x *
*---------------------------------------------------------*
* 0x7fff4ee20960 | 0x7fff4ee2095c | p_x *
*---------------------------------------------------------*
* 0x7fff4ee20968 | 0x7fff4ee20960 | p_px *
*---------------------------------------------------------*