Pointers in C Programming

A pointer in C programming language is a variable which is used to store the address of another variable. It is one of the most powerful features of the C programming language. Pointers are used everywhere in the C language. Once you master the use of pointers, you will use them everywhere to make the code more efficient and faster.
Some operations in C, can be performed efficiently using pointers like dynamic memory allocation, dynamic data structures like linked list and trees, pass arguments to a function as Call by Reference, access array elements etc.
To become an expert C programmer, it is necessary to have deep understanding of pointers.


What Are Pointers

A variable in C is the name given to a memory location, where a program can store data. A program can store and manipulate the value stored using variable's identifier. When we declare a variable in C, compiler reserve space in memory to hold the value assigned to this variable.
We can access the value of this variable either by variable identifier or by directly accessing the memory location using pointers. Every memory location is given a unique address, once you know the address of a memory location(variable), you'll then be able to go to that address and retrieve the data stored in it. To get the address of any variable, we can use &(Address of) operator and to retrieve the value stored at a memory location we can use *(Value of Operator).


Advantages of Pointers

  • We can dynamically allocate or deallocate space in memory at run time by using pointers.
  • Using pointers we can return multiple values from a function.
  • We can pass arrays to a function as call by Reference.
  • Pointers are used to efficiently access array elements, as array elements are stored in adjacent memory locations. If we have a pointer pointing to a particular element of array, then we can get the address of next element by simply incrementing the pointer.
  • Pointers in C are used to efficiently implement dynamic Data Structures like Queues, Stacks, Linked Lists, Tress etc.
  • The use of pointers results into faster execution of program.

Pointer Operators

While handling a data through pointers, we need to know the address of a variable and second is given a memory address or pointer we want to access data stored at that location. Address of(&) and Value of(*) operators are used to perform above mentioned pointer operations respectively.

Address of Operator(&)

The & is a unary operator in C which returns the memory address of the passed operand. This is also known as address of operator. A pointer contains the memory address of some object.

Value of Operator(*)

The * is a unary operator which returns the value of object pointer by a pointer variable. It is known as value of operator. It is also used for declaring pointer variable.
For Example

    int A = 100;
    int *ptr = &A;

In the first statement, we first declare an integer variable and initialize it with value 100. In second statement, we are declaring a pointer to a variable of type int and initializing it with address of A.


Pointer Declaration

A pointer is a derived data type that is created from fundamental data types. We use (*) for defining pointer variables. Here is the syntax of declaring a pointer variable.

<data_type> *<identifier>;
For Example
    int *ptr;

Above pointer declaration specifies that, ptr is a pointer variable that will store the memory address where an integer value is stored.
We can also initialize a pointer as follows:

    int count = 10;
    int *ptr = &count;

Integer variable 'count' is the name to a memory location where value 10 is stored. Let the memory address of count is "52436721". Then pointer variable ptr will contain address of count variable that is "52436721"

#include <stdio.h>
#include <conio.h>

int main () {
   int  count = 10;
   int *ptr = &count;

   printf("Value of count variable is %d\n", count);
   printf("Address of count variable: %x\n", ptr);
   printf("Value retrieved through pointer : %d\n", *ptr);

   getch();
   return 0;
}

Output
Value of count variable is 10
Address of count variable: 22fe44 
Value retrieved through pointer : 10

NULL Pointers in C

NULL pointer in C is a pointer which is pointing to nothing. It is used to initialize a pointer at the time of declaration if we don't have any explicit value to initialize. It is a good practice to initialize a pointer with NULL to ensure that it is not pointer to a random memory location. The NULL is a macro constant with a value of zero defined in various C header files like stdio.h, stdlib.h, alloc.h etc.

#define NULL 0
A pointer initialized with NULL is known as NULL pointer.
int *ptr = NULL;

Pointer ptr is initialized with NULL. Pointer ptr is not pointing to any valid memory location. We can check whether a pointer is a NULL pointer or not as follows:

if(ptr == NULL)
if(!ptr) 
#include <stdio.h>
#include <conio.h>

int main(){
   int *ptr = NULL;
   printf("The value of ptr is %x\n", ptr);
   
   if(ptr == NULL){
        printf("ptr is NULL Pointer\n");
   }
   
   if(!ptr){
        printf("ptr is NULL Pointer\n");
   }
   getch();
   return 0;
}

Output
The value of ptr is 0
ptr is NULL Pointer
ptr is NULL Pointer

Size Of Pointer Variable

Size of a pointer variable is system dependent. A memory address is considered as integer value. Size of a pointer is fixed, it doesn't depend on the data type it is pointing to. We can use size of operator to get the size of a pointer.

#include<stdio.h>
#include<conio.h>

int main() {
    int count = 10;
    float sum = 20.5;
    int *null_ptr = NULL;
    int *int_ptr = &count;
    float *float_ptr = &sum;
    
    printf("Size of NULL Pointer : %d Bytes\n", sizeof(null_ptr)); 
    printf("Size of Integer Variable : %d Bytes\n", sizeof(count));
    printf("Size of Integer Pointer : %d Bytes\n", sizeof(int_ptr));
    printf("Size of Float Variable : %d Bytes\n", sizeof(sum));
    printf("Size of Float Pointer : %d Bytes\n", sizeof(float_ptr));

    getch();
    return 0;
}

Output
Size of NULL Pointer : 8
Size of Integer Variable : 4
Size of Integer Pointer : 8
Size of Float Variable : 4
Size of Float Pointer : 8