What are Pointers in C and its Types?

In this article we will learn about pointers in C with examples. We will also learn about the types of pointers in C with examples.

What are Pointers in C

Pointers in C are variables which store the address of the memory location assigned to a variable. A Pointer variable in C can also the address of another pointer variable.

To point the previous memory location or next memory location, a pointer can be incremented or decremented respectively.

We can save memory and also achieve faster execution time using pointers in C.

To get the address of the memory location located to a variable directly we can use ampersand (&) operator with the name of the variable.

Whenever we declare a pointer, the compiler assigns the memory location for that particular variable and the value of the variable will be stored at that particular location. We can access this value using the variable name and the address of the memory location of the variable using ampersand (&) operator with the name of the variable.

How to Declare Pointers in C

data_type *var_name;

data_type is the same as the data type of the variable whose address will be stored in the pointer variable(var_name). If the pointer variable stores the address of the memory location of the integer variable, then the data type of the pointer variable will be integer too.

int    *pntrin;     // pointer to an integer 
char   *pntrch ;    // pointer to a character 
double *pntrdbl;    // pointer to a double 
float  *pntrfl;     // pointer to a float

*Use of (asterisk) while declaring the pointer variable is must.**

data_type *var_name;

This syntax will store the address of the memory location of the variable with the same data type as the pointer variable.
To store the address of a pointer variable into the another pointer, you have to declare that pointer variable as:

data_type **var_name;

If a pointer variable declared with 3 as asterisk(), then that means it will store the address of the pointer of pointer of pointer.
Therefore, to know what types of pointer is declared, can be identified by data_type and total number of asterisk(
) present at the declaration.

Note:
A variable whose memory address is stored in a pointer must have some value;
The address of a variable is stores using ampersand (&) operator

int *ptr;      //pointer variable declaration
ptr = &x;       //store address of variable x in pointer ptr

Using *(asterisk) operator, we can get the value store at the memory address pointed by the pointer variable, using pointer.

Example explaining pointers in C:

#include <stdio.h>
int main()
{
   int v = 42;     //variable declaration (integer  type)
   int *pntr;      //declaration 0f pointers C (integer  type)
   pntr = &v;       //store address of variable v in pointer ptr
   //printing the address value 
   printf("Address stored in a variable ptr is: %x \n", pntr);
   //printing the value using asterisk(*) with pointer variable
   printf("Value stored in a variable ptr is: %d \n", *pntr);   
   return 0;
}

Direct and Indirect Access

The two ways to access and manipulate the value stored in a memory location.

1. Direct Way:
In this we will use variable name to access and manipulate the value.

2. Indirect Way:
In this we will use pointer variable pointing to the memory location of value with asterisk(*) operator.

Example of Direct and Indirect ways(Pointers in C):

#include <stdio.h>
int main()
{
   int v = 42;     //variable declaration (integer  type)
   int *pntr;      //declaration of pointers in C(integer  type)
   pntr = &v;       //store address of variable v in pointer ptr
   //printing the value using direct access
   printf("Value stored in a variable ptr using direct access: %d \n", v);
   //printing the value using indirect access
   printf("Value stored in a variable ptr using indirect access: %d \n", *pntr);   
// accessing address of variable two ways
   printf("Address stored in a variable ptr is: %x \n", &v);
   printf("Address stored in a variable ptr is: %x \n", pntr);
// changing the value stored in variable ‘v’ using pointer
*pntr=88;
   printf("Value stored in a variable ptr using indirect access: %d \n", *pntr); z
   return 0;
}

Types of Pointers in C

Types of Pointers in C are

NULL Pointers in C:

If a Pointer in C is initialized with NULL address, then that pointer will be called NULL Pointer in C. The value of NULL Pointers in C is always 0.

Example of NULL Pointers in C

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

Output
The value inside variable ptr is: 0

Void Pointers in C

The Void Pointers in C do not have any standard data type like int, char, double etc. Void Pointers in C are created using the ‘void’ keyword. Void Pointers speciality is that they can store address of any data type. Void Pointers are also called Generic Pointers.

Example of Void Pointers in C

#include<stdio.h>
int main() {
  void *pntr = NULL; //void pointer
 char c = ‘a’;
  pntr = & c;
  //typecasting Void Pointer to char  using (char *)
  printf("value of *ptr is %c", *(char* ) pntr);  
  return 0;
}

Output
value of *ptr is a

Wild Pointers in C

A pointer which is not initialized with any address or with NULL are called Wild Pointers in C. One should avoid these types of pointers in C because they may have some memory address which is unknown which can cause the crashing of the program. These types of pointers in C are not efficient, so we should avoid using them.

Example of Wild Pointers in C

#include <stdio.h>
int main() {
 //initializing wild pointer in C
int *pntr;
  printf("value of *ptr is %d ", *pntr);  
  return 0;
}

Output
Runtime Error

Dangling Pointers in C

A pointers which points to a memory location which is released are called Dangling Pointers in C.

Example of Dangling Pointers in C

#include <stdlib.h>
#include <stdio.h>
int main() {    
  //memory allocated to ptr
  int *ptr = (int* ) malloc(sizeof(int));  
  // memory released
  free(ptr);  
  // ptr is pointing to a deleted memory location now.
  // now ptr became dangling pointer
  printf("value of *ptr is %d ", *ptr);  
  // Removing dangling pointer
  ptr = NULL;
}

Output
value of *ptr is 0

Some other types of Pointers in C

  1. Complex Pointers in C
  2. Near Pointers in C
  3. Far Pointers in C
  4. Huge Pointers in C

Note About Precedence Order of Operators
*(asterisk) and &(ampersand) operators have same precedence order as any other unary operators like incrementation(++) and decrementation(–).
If they are used in the same expressions, then these unary operators are evaluated left to right.

Leave a Reply

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