Pointer Variable
An address of a variable can be stored in a special variable called Pointer Variable. A Pointer variable is a variable that contains the address of another variable. Let us assume that y is such a variable.
Now the address of variable x can be assigned to y by the statement:
int x = 10;
y = &x;
It is clear that y is the variable that contains the address (i.e. 2002) of another variable x, whereas the address of y itself is 4004(assumed value).
In other words, we can say that y is a pointer to variable x.
Consider the following segment: –
:
printf("\n Value of x = %d", x);
printf("\n Address of x = %d", &x);
printf("\n Value of x = %d", *y);
printf("\n Address of x = %d", y);
printf("\n Address of y = %d", &y);
:
Once the segment is obeyed the output would be: –
Value of x = 10
Address of x = 2002
Value of x = 10
Address of x = 2002
Address of y = 4004
Syntax for Pointer declaration in C is:
Free Hand-Written Notes
data_type*<pointer_name>;
e.g. A Pointer Variable y can be declared in C as
int *y;
This Declaration means that y is a pointer to a variable of type int. Consider the following Declaration:
This declaration means that p is a Pointer to a variable of type char.
Let us now consider the following statements: –
:
float v = 3.65;
float *t;
:
The first statement declares v to be a float type variable and initializes it by value 3.65.
The Second statement declares t to be pointer to a variable of type float.
Please notice that t can be point to a variable of type float but is not currently pointing to any variables as shown below: –
The Pointer t can point to the variable v by assigning the address of v to t.
t = &v;
Once this is obeyed, pointer t gets the address of v and we can say that logically t points to v as shown below: –