The pointer is a variable that stores the address of another variable.
The syntax for the pointer is as follows −
pointer = &variable;
Types of Pointers
There are eight different types of pointers which are as follows −
Null pointer
Void pointer
Wild pointer
Dangling pointer
Complex pointer
Near pointer
Far pointer
Huge pointer
Null Pointer
You create a null pointer by assigning the null value at the time of pointer declaration.
This method is useful when you do not assign any address to the pointer. A null pointer always contains value 0.
Example
Following is the C program for the null pointer −
#include <stdio.h> int main(){ int *ptr = NULL; //null pointer printf("The value inside variable ptr is:\n%d",ptr); return 0; }
Output
When the above program is executed, it produces the following result −
The value inside variable ptr is: 0
Void Pointer
It is a pointer that has no associated data type with it. A void pointer can hold addresses of any type and can be typecast to any type.
It is also called a generic pointer and does not have any standard data type.
It is created by using the keyword void.
Example
Following is the C program for the void pointer −
#include <stdio.h> int main(){ void *p = NULL; //void pointer printf("The size of pointer is:%d\n",sizeof(p)); //size of p depends on compiler return 0; }
Output
When the above program is executed, it produces the following result −
The size of pointer is:8
Wild Pointer
Wild pointers are also called uninitialized pointers. Because they point to some arbitrary memory location and may cause a program to crash or behave badly.
This type of C pointer is not efficient. Because they may point to some unknown memory location which may cause problems in our program. This may lead to the crashing of the program.
It is advised to be cautious while working with wild pointers.
Example
Following is the C program for the wild pointer −
#include <stdio.h> int main(){ int *p; //wild pointer printf("\n%d",*p); return 0; } Process returned -1073741819 (0xC0000005) execution time : 1.206 s Press any key to continue i.e. you won’t get output, some compilers show error message at output