Pointer
A pointer is a special variable that stores the memory address of another variable. A pointer also needs to be declared in a program but cannot store data instead it stores the memory address of a variable.
Eg:
Let us declare an integer variable:
Int a;
Then the corresponding pointer variable which can point to a memory address of integer variable should be of
integer type.
i.e. int*p;
here, ‘p’ is a pointer variable that can store the address of ‘a’ but it cannot store the address of another type variable. For example, P cannot store the address of the float variable.
Address operator(&):
The unary operator which returns the address of a variable, is known as address operator.
Declaring pointer and pointer variable
The syntax of declaring a pointer variable is:
Data_type*variable_name;
Eg:
Int*p;
Here, ‘p’ is an integer pointer which holds the memory address of an integer variable.
Char*c;
Here, ‘c’ is a character pointer which holds the memory address of a character variable.
Float*d;
Here, ‘d’ is a float pointer which holds the memory address of a float variable.
REFERNCING AND DEREFERENCING
Referencing of a pointer refers to the process of making a pointer variable point to another variable by providing an address of that variable to the pointer variable.
Pointer initialization
Syntax:
Pointer variable = &normal_variable;
Eg:
Int a=57;
Int*p;
P=&a;
DERDFERENCING OF A POINTER
It is a process where ‘*’ is used with the pointer variable to return the value of the normal variable pointed by the pointer variable.
Syntax:
*pointer_variable;
Eg:
#include<stdio.h>
Void main()
Int*p, a=20;
P=&a;
Printf(“the address of a is: %d”, &a);
Printf(“\n the value of p is: %d”, p);
Printf(“\n the value of a is: %d”, a);
Printf(“\n the value of *p is: &d”, *p);
}
ADVANTAGE OF POINTER
The advantage of pointers are listed below:
- Pointer make program simple and also saves memory space.
- It enhance the execution speed of a program as they refer to the address.
- Pointers are useful for accessing memory locations.
- It is used for dynamic memory allocation and deallocation during the execution of a program.
- Pointer is closed to the array so it is efficient for solving array and string related problems.