Introduction
Pointers are a type of data in C; hence we can also have pointers to pointers, just as we have pointers to integers. Pointers to pointers offer flexibility in handling arrays, passing pointer variables to functions, etc.
The general format for declaring a pointer to a pointer is.
<datatype> **<ptr-to-ptr>;
Which uses two asterisks (**) symbols placed one beside the other. Thus, the declaration implies that the variable <ptr-to-ptr> is a pointer to a pointer to a pointer to a data object of the type <datatype>.
Importance of Pointer to Pointer with example
This feature is often made use of while passing two (or higher) dimensional arrays back and forth among different functions.
Write a program that declares and uses pointers to pointers.
Source Code
#include <stdio.h>
void main()
{
int *iptr;
int**ptriptr;
int data;
iptr=&data; //iptr now points to data //
ptriptr=&iptr; //ptriptr points to iptr //
*iptr=100; // same as data=100 //
printf("variable data contains : %i/n",data);
**ptriptr=200; // same as data=200 //
printf("variable data contains : %i/n",data);
data=300:
printf("ptriptr is pointing to : %i/n",**ptriptr);
getch();
}
Output
Pointers and Arrays
Pointers are intimately associated with arrays. An array in C is evaluated as a constant pointer and therefore, we do not use the address operator with an array name. When an array is declared, the compiler allocates a base address (address of the 0th element) and a sufficient amount of storage to contain all the elements of the array in contiguous memory locations. The compiler also defines the array name as a constant pointer with reference to the first elements of the array.
In C if array is the name of an integer array and C is an integer variable, then the expression
array=&i;
Is an invalid expression in C, we have used a constant on the left-hand side of the expression.
The C compiler stores all the elements of an array, contiguously i.e. adjacent to one another, and all the elements of the array are of some data type (by array definition), we only require the address of the first element of the array to reference the entire array.
Consider, we declare an array of integers of integer type.
int array [5];
Let us assume that an int takes up two bytes of the memory, also let us assume that the base address of the array is 1000. This means that the address of array [0] is 1000. Since an int takes two bytes of storage space in the memory, the next element of the array, array [1] will be located at 1002.
Similarly, all the other elements of the array will be located in the memory.
Write a program to illustrate the use of pointers with arrays.
Source Code
#include <stdio.h>
void main()
{
static int array[]={1,2,3,4,5}
int *p, i;
p=array; //the declaration is same as p=&array[0] to store bade address of the array//
for(i=0;i<5;i++)
printf("array[%2d]=%2d\n",i,p[i]); // using the pointer to access array //
getch();
}
Output
The relationship between p and elements of an array is given below.
P = &array[0]
P + 1 = &array[1]
P + 2 = &array[2]
P + 3 = &array[3]
P + 4 = &array[4]
When handling arrays, instead of using array indexing, we can use pointers to access array elements.
For example, the statements *(p+2) would refer to the array[2] elements and would return the values of the third element of the array.
Write a program that illustrates pointers with arrays.
Source Code
int first_array[2]={10,20};
#include<stdio.h>
#include<conio.h>
void main()
{
int *second_array=first_array; // second_array is a pointer to the first_array //
second_array[0]=100;
clrscr();
printf("first_array=(%d,%d)/n",first_array[0],first_array[1]);
getch();
}
Output
Summary
Pointers to pointers are variables that store the address of another pointer, allowing for multiple levels of indirection in memory access. In relation to arrays, pointers can be used to traverse arrays efficiently, and a pointer to an array is essentially a pointer to its first element, enabling operations on the array using pointer arithmetic.