Pointer in C

A pointer is a variable that contains the address of another variable.

Declaration:

dataType *PointerName ;

Here, dataType must be a valid C data type and PointerName is the variable name.

Examples:

int * i; // i is a pointer to an integer
double *d; //d is a pointer to a double
float *f; // f is a pointer to float
char *c; // c is a pointer to char

Why pointer?

Pointers are useful for accessing memory locations, it is used for dynamic memory allocation and deallocation.

Operators used while creating or accessing pointers:

  • asterisk operator (*)
    • Used in declaration of pointer.
    • Returns the value of the referenced variable. 
  • ampersand operator (&):
    • Returns the address of a variable.

Note: To Access the value stored in the variable using pointer, we use Asterisk (*) before pointer variable. This is called dereferencing.

Code:

C Program

#include<stdio.h>

int main(){
    int a = 5;  //a is a normal variable
    int *i;  //i is a pointer variable.
    i = &a; //address of a is assigned to i 

    printf("%d\n",a);  //->it prints value of a 
    printf("%d\n",&a);  //->it prints address of a
    printf("%d\n",i);  //->it prints address of a 
                       // which is stored in pointer i
    printf("%d\n",(*i)); //->it prints the value which 
                         // is pointed by pointer i

    return 0;
}

Pointer to Pointer:

A pointer stores the address of another variable. When a pointer stores address of another pointer and that pointer contains the address of another variable, then this is called pointer to a pointer.

Example:

The address of variable var is stored in pointer P2. The address of P2 is stored in pointer P1. 

So, 

printf(“%d\n”,*P1);  //->Prints address of P2 or the value of P1
printf(“%d\n”,*P2) ; //->Prints the value of Var
printf(“%d\n”,**P1); //->Prints the value of var 

Pointer Arithmetic

Pointer Arithmetic is useful for arrays. There are four arithmetic operators that can be used on pointer –

  • Increment (++)
  • Decrement (–)
  • Addition (+)
  • Subtraction (-)

Increment

C Program

#include<stdio.h>

int main(){
int arr[]={1,2,3,4,5};
int i = 0;
int *p = &arr; // the base address of array is assigned pointer p
for(i=0;i<=4;i++){
printf("The address of arr[%d] is %d \n",i,p);
printf("The value of arr[%d] is %d \n",i,*p);
p++; //p points to next element, means p holds the address of next element
}
return 0;
}

Output:

The address of arr[0] is 677719568
The value of arr[0] is 1
The address of arr[1] is 677719572
The value of arr[1] is 2
The address of arr[2] is 677719576
The value of arr[2] is 3
The address of arr[3] is 677719580
The value of arr[3] is 4
The address of arr[4] is 677719584
The value of arr[4] is 5

Decrement

C Program

#include <stdio.h>

int main()
{
    int arr[]={1,2,3,4,5};
    int i = 0;
    int n = sizeof(arr)/sizeof(arr[0]);
    int *p = &arr[n-1];
    for(i=n-1;i>=0;i--){
        printf("The address of arr[%d] is %d\n",i,p);
        printf("The value of arr[%d] is %d\n",i,*p);
        p--; // p points to previous element of the array
    }
    return 0;
}

Output:

The address of arr[4] is 1586392624
The value of arr[4] is 5
The address of arr[3] is 1586392620
The value of arr[3] is 4
The address of arr[2] is 1586392616
The value of arr[2] is 3
The address of arr[1] is 1586392612
The value of arr[1] is 2
The address of arr[0] is 1586392608
The value of arr[0] is 1

Addition with integer

C Program

#include <stdio.h>

int main()
{
    int var = 10;
    int *p1 = &var; //assume the address of var is 100
    
    printf("Before the addition, the value of p1 = %d\n",p1);
    
    p1 = p1+2; //since, integer takes 4 bytes, so p1 actually added by 4*2=8 
    
    printf("After the addition, the value of p1 = %d\n",p1);
    return 0;
}

Output:

Before the addition, the value of p1 = 1479721596
After the addition, the value of p1 = 1479721604

Subtraction with integer

C Program

#include <stdio.h>

int main()
{
    int var = 10;
    int *p1 = &var; //assume the address of var is 100
    
    printf("Before the subtraction, the value of p1 = %d\n",p1);
    
    p1 = p1-2; //since, integer takes 4 bytes, so, p1 will be subtracted by 4*2=8
    
    printf("After the subtraction, the value of p1 = %d\n",p1);
    return 0;
}

Output:

Before the subtraction, the value of p1 = 1962483372
After the subtraction, the value of p1 = 1962483364

Types of Pointer:

  1. Null Pointer – When we assign null value at the time of pointer declaration, then that pointer is called Null pointer.

Example – 

int * p = NULL; //when we print the value of ptr, it prints 0
  1. Void Pointer – A void pointer can hold the address of any data type and can be typecast to any type.

Example – 

int n = 5;
void * ptr ;
ptr = &n; //it can hold the address of any data type.
  1. Wild pointer – If a pointer is uninitialized , then it is called wild pointer. This may lead to crashing the program.

Example – 

int *p; //it’s not initialized.

Special thanks to Bikram Das for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article