A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable.
A pointer provides a way to access a variable without referring to the variable directly.
Restriction on pointers is they are not allowed to store any memory address, but they can only store addresses of variables of a given type.
Consider the following example to define a pointer which stores the address of an integer.
int* p = &n; // Variable p of type pointer is pointing to the address of the variable n.
The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.
The ‘*’ operator (indirection operator) is used in two distinct ways with pointers
- used for pointer declaration.
- used for dereferencing.
Used as dereferencing operator ‘*’ indicates thatthe value at the memory location is to be accessed.
void main( )
{
int a=10;
int *p;
p=&a;
printf(“value of a %d”, a);
printf(“value of a %d”,*p);
printf(“address stored in p is %u”, p);
}
Features of Pointers:
- Pointers save memory space.
- Execution time with pointers is faster because data are manipulated with the address, that is, direct access to memory location.
- Memory is accessed efficiently with the pointers. The pointer assigns and releases the memory as well. Hence it can be said the Memory of pointers is dynamically allocated.
- Pointers are used with data structures. They are useful for representing two-dimensional and multi-dimensional arrays.
- An array, of any type can be accessed with the help of pointers, without considering its subscript range.
- Pointers are used for file handling.
- Pointers are used to allocate memory dynamically.
0 comments:
Post a Comment