Introduction:-
A pointer is a special variable that is used to store the address of some other variable. A pointer can be used to store the address of a single variable, array variable, structure, union, or even a pointer.
The concept of pointers:-
Every variable is stored in the memory, and each memory location has a numeric address. The declaration of the variable int a = 5; Here a is the name of the variable, the value of the variable is 5 while the address of the variable is 100 (assumed).
The declaration of the variable tells the compiler to :-
1. Reserve space in the memory to hold the integer value.
2. Associates the name “a” with this memory location.
3. Stores the value 5 at this location.
Pointers and the indirection operator:-
The two fundamental operators used with the pointers are:
1. address operator &
2. indirection operator *
Example
main( )
{
int a = 5;
int *p; /*pointer declaration*/
p= &a; /*copying address of variable a to the pointer p*/
*p = 10; /*indirection or use of pointer to Change the value of variable a*/
printf(“%d”, a);
printf(“%d”,*p);
printf(“%d”,*(&a));
}
All the printf statements in the above program will give the output as 10. Since the variable p is not an ordinary variable like any other integer variable. It is A variable which stores the address of some other variable (a in this case). Since p is a variable, the compiler must provide memory to this variable also. Any type of pointer gets two bytes in the memory. int *p1; float *p2; char *p3; all pointers p1, p2, p3 get 2 bytes each.