Pointer in Data Structure

In this tutorial, we will discuss the Pointers in Data Structure in detail with program examples for better understanding. Pointers enhances the performance of repetitive operation in Data Structure such as Traversing through a String, Searching Tables, Manipulating Tables, and Tree Strcutures.

Now, let’s start with the introduction of Pointers in Data Structure.

What is Pointer in Data Structure?

A Pointer is a derived data type that stores the address of another variable. A Pointer contains memory addresses as their values. Pointers can be used to assign, access, and manipulate data values stored in the memory allotted to a variable since it can access the memory address of that variable.

  • Here, the value in Memory Address is the address of the Variable in Memory.
  • We can access the value at Memory Address 1003 by using the adddres stored as value at Memory Address 1000.
  • Hence, we can say that Memory Address 1000 points to the Variable in Memory at Memory Address 1003. Thus, variable at Memory Address 1000 is a pointer variable.
  • The values of variable and memory address can be different after each program run.

Below is an example to demonstrate how we can access a variable through its Pointer.

Example: Program to illustrate the use of indirection operator ‘*’.

Code:

#include<stdio.h>
int main()
{
    int x=2,y;
    int *ptr;
    ptr = &x;
    y = *ptr;
    printf("Value stored in x is %d\n",x);
    printf("Address of %d is %u\n",x,&x);
    printf("Address of %d is %u\n",*&x,&x);
    printf("Address of %d is %u\n",*ptr,ptr);
    printf("Address %d is stored at %u\n",ptr,&ptr);
    printf("Address of %d is %u\n",y,&y);
    *ptr = 25;
    printf("Now the value stored in x is %d\n",x);
    return 0;
}

Output:

Value stored in x is 2
Address of 2 is 890966264
Address of 2 is 890966264
Address of 2 is 890966264
Address 890966264 is stored at 890966272
Address of 2 is 890966268
Now the value stored in x is 25

Advantages of using Pointer in Data Structure

Pointers are so useful that it is the most frequently used feature in Data Structure, because of its numerous advantages.
Some of them are listed below:

  • Pointers are most efficient in handling arrays and other data structures.
  • Pointers can return multiple values from a function through function arguments.
  • Pointers can be used to reference a function and hence it enables passing of a function as an argument to another function.
  • Pointers save a lot of space in storage memory by offering the use of pointer arrays to character strings.
  • Pointers support dynamic memory allocation in Data Structure.
  • Pointers enables a user to manipulate dynamic data structures such as linked lists, queues, stacks, and trees.
  • Pointers drastically reduce the complexity and length of a program.
  • Pointers help in reducing the execution time by increasing the execution speed of a program.

Arithmetic Operations of Pointers

We can add or subtract integers from a pointer and we can subtract one pointer from another pointer too. Example: p1 + 4p2 – 2, and p1 – p2.

We can also use other operators with pointers, for example:

p1++;

-p2;

sum += *p2;

p1 > p2

p1 == p2

p1 != p2

But we cannot multiply or add two pointers, for example: p1 * p2 and p1 + p2 are not allowed.

Below is an example to demonstrate how we can use Pointers in expressions.

Example: Program to illustrate the use of Pointers in arithmetic operations.

Code:

#include<stdio.h>
int main()
{
  int a=8,b=5,x,y,z,*p1,*p2;
  p1 = &a;
  p2 = &b;
  x = *p1 * *p2 - 3;
  y = 4* - *p2 / *p1 + 5;
  printf("Address of a is %u\n",p1);
  printf("Address of b is %u\n",p2);
  printf("a is %d, b is %d, x is %d, y is %d\n",a,b,x,y);
  *p2 = *p2 + 3;
  *p1 = *p1 * *p2 - 6;
  z = *p1 * *p2 - 6;
  printf("a is %d, b is %d, z is %d\n",a,b,z);
  return 0;
}

Output:

Address of a is 3576265156
Address of b is 3576265160
a is 8, b is 5, x is 37, y is 3
a is 58, b is 8, z is 458

Pointers and Arrays

Pointers prove to be very useful for accessing elements present in an Array through the address of each cell of Array.

  • When an Array is declared, sufficient memory is allocated for the storage of all the Array elements in contiguous memory locations.
  • Also, after an Array is declared, a base address is allocated to the first element of the Array (index value 0).
  • An Array name is also defined as a constant pointer to the first element.

Example:

int x[5] = {1,2,3,4,5};

Let’s say, the base address of the first element of x (x[0] = 1) is 3000, now since Array is of type inteach integer will require 4 bytes.
So, the elements will be stored in a way like this:

The name x is defined as a pointer constant pointing to the first element of Array i.e. x[0], therefore the value of pointer constant x is 3000 (address of x[0]).

x = &x[0] = 3000

Now, if we declare p as an integer pointer to point to the Array x,

p = x;
or
p = &x[0];

Now, we can access every value of constant pointer x using the increment operator (++) with integer pointer p by moving from one element to another through increasing the addresses.

p = &x[0] (= 3000)
p+1 = &x[1] (= 3002)
p+2 = &x[2] (= 3004)
p+3 = &x[3] (= 3006)
p+4 = &x[4] (= 3008)

Now, finally we can access every value present in the origin of Array elements using:

*(x+i) or *(p+i)

*x = 1  or *p = 1
*(x+1) = 2 or *(p+1) = 2
*(x+2) = 3 or *(p+2) = 3
*(x+3) = 4 or *(p+3) = 4
*(x+4) = 5 or *(p+4) = 5

Below is an example to demonstrate how we can access Array elements using the Pointer.

Example: Program to find the sum of all the elements of an Array using Pointers.

Code:

#include<stdio.h>
int main()
{
    int *p,sum=0,i=0;
    int x[3]={1,3,5};
    p = x;
    while (i<3)
    {
        printf("Value of element x[%d] is %d stored at address %d.\n",i,*p,p);
        sum = sum + *p;
        i++;
        p++;
    }
    printf("Sum of all the elements is %d.\n",sum);
    return 0;
}

Output:

Value of element x[0] is 1 stored at address 181672540.
Value of element x[1] is 3 stored at address 181672544.
Value of element x[2] is 5 stored at address 181672548.
Sum of all the elements is 9.

What is chain of Pointers?

Chain of pointers is created when we make a pointer to point to another pointer and it may continue further.

Here, pointer contains the address of the another pointer variable, which contains the address of the original variable having the desired data value. This is known as multiple indirections.

A pointer that points to another pointer must be declared with an extra unary operator (i.e.’*’ an asterisk).

Example:

int **p2;

  • Here, pointer variable p2 points to another variable p1 of type int.

We can access the target value through chain of operators by applying indirection operator ‘*’ twice.

Example:

main()

{

   int x, *p1, **p2;

   x = 100;

   p1 = &x;

   p2 = &p1;

   printf(“%d”, **p2);

}
  • Here, p1 is declared as pointer to an integer and p2 as a pointer to a pointer to an integer.
  • Hence, the output will be value 100 stored in variable x.

Passing address to Functions

We can pass the address of a variable as an argument to a function. We have used this method in returning multiple values in Function in the C programming tutorial.

In calling a function, we can use two methods to pass the values of a variable to a function definition from the function call.
Below are those two methods discussed.

The function which is called by reference can modify the values of variables used in the call. This method is also known as call by address or pass by pointers.

Now, we will study an example to understand how the addresses of variables are passed to a function using pointers to change the values of these variables stored in two different locations in the memory.

Example: Program to swap values of two variables using pointer operation.

Code:

#include <stdio.h>  
void swap(int * , int *); 
int main()  
{  
    int x = 2;  
    int y = 4;   
    printf("x is %d and y is %d before swapping\n",x,y);
    swap(&x,&y);
      
    printf("x is %d and y is %d after swapping\n",x,y);
    return 0;
}  
void swap (int *x, int *y)  
{  
    int temp;   
    temp = *x;  
    *x=*y;  
    *y=temp;
}

Output:

x is 2 and y is 4 before swapping
x is 4 and y is 2 after swapping

Returning Pointers from Functions

In Functions in the C programming tutorial, we have learnt that a Function can return a single value by its name. And also it can return multiple values through pointer parameters.

In C programming language, we can return a Pointer from the Function definition to the calling Function. We will understand the working of such a program through an example:

Example: Program to find a larger number between two numbers.

Code:

#include<stdio.h>
int *Large(int *, int *);
int main()
{
  int a=5,b=10,*p;
  p = Large(&a,&b);
  
  printf("Larger number between %d and %d is %d.",a,b,*p);
}
int *Large(int *x, int *y)
{
  if(*x>*y)
    return (x);
  else
    return (y);  
}

Output:

Larger number between 5 and 10 is 10.

In the next section of the tutorial, we will discuss the use of Structure in Data Structure and we will understand how it improves the accessibility of using variables through pointers in Data Structure operations.