Types of Pointers in C

A pointer is a variable pointing to the address of another variable. It is declared along with an asterisk symbol (*). A pointer can also be used to refer to another pointer function. A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location. The purpose of pointer is to save memory space and achieve faster execution time.

There are majorly 3 types of pointers, they are:

  • Null Pointer
  • Void Pointer
  • Wild Pointer

1 . Null Pointer:-

  • A NULL pointer is a pointer that is pointing to nothing, i.e. it is assigned a null value.
  • In case, if you don’t have an address to be assigned to a pointer then you can simply use NULL. (It is considered a good practice to set it to null.)
  • The pointer which is initialized with NULL value is considered as a NULL pointer.

Following program illustrates the use of a null pointer:

#include <stdio.h>
int main()
{
	int *p = NULL; 	//null pointer
	printf(“The value inside variable p is:\n%x”,p);
	return 0;
}

Output:

The value inside variable p is:
  0

2 . Void Pointer:-

In C programming, a void pointer is also called as a generic pointer. It does not have any standard data type. A void pointer is created by using the keyword void. It can be used to store an address of any variable.

Following program illustrates the use of a void pointer:

#include <stdio.h>
int main()
{
void *S = NULL; 	//void pointer
printf("The size of pointer is:%d\n",sizeof(S));
return 0;
}

Output:

The size of pointer is:4

3 . Wild Pointer:-

A pointer that has not been initialized to anything till its first use (not even NULL) is known as a wild pointer. The pointer may be initialized to a non-NULL garbage value that may not be a valid address.
Wild pointer is also called Bad pointer because without assigning any variable address, it points to the memory location.

Following program illustrates the use of wild pointer:

#include <stdio.h>
int main()
{
int *p; 	//wild pointer
printf("\n%d",*p);
return 0;
}

Output:-

timeout: the monitored command dumped core
sh: line 1: 95298 Segmentation fault timeout 10s main

Leave a Comment

Your email address will not be published. Required fields are marked *

// Sticky ads
Your Poster