Variables
Variables in programming languages are abstraction of memory cells.
Variable has three attributes:
- name,
- type,
- value.
One more attribute is its location in memory:
- address.
Pointer - address variable
A type of variable that contains the memory address of other variable, also called address variable.
For example:
int b;
int * a;
When running a program containing these two declaration statements, computer will allocate memories for variables a and b in locations 0002 and 1008 respectively.
b = 10;
makes b containing integer 10.
b = 123;
makes b containing integer 123.
In the same way,
a = &b;
makes a containing b's address 1008, which means a points to b. And suppose int c; c is located in 1009, then
a = &c;
makes a containing c's address 1009, which means a points to c.
reference and dereference
After
a = &b;
We could use *a to refer to variable b. this is called dereferencing. also called indirected addressing.
Hereafter, we can use *a and b interchangably.