A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant
Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect C programmer.
Some of the valid pointer declarations -
|int *ip; /* pointer to an integer */|
|double *dp; /* pointer to a double */|
|float *fp; /* pointer to a float */|
|char *ch /* pointer to a character */|
Example - create pointer
It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.
Example - null pointer
The pointer variable to get the memory address of a variable used together with the & reference operator. The value of the variable the pointer points to, by using the * operator is the dereference operator.
Example - dereference
- When used in declaration (int* ptr), it creates a pointer variable.
- When not used in declaration, it act as a dereference operator.