Pointer to Pointer in C
Notes:
Pointer to Pointer (Double Pointer) in C Programming Language:
- understand how to create a pointer variable; which holds address of another pointer variable
- understand how to get or set the value at another pointer variable is pointing
Note: a pointer to a pointer is called double pointer
Declaring and initializing a pointer to a pointer:
Syntax:
data_type **variableName = address of another pointer variable or NULL;
Ex:
int **iptr =NULL;
// iptr is a pointer variable; which can hold address of another integer type pointer variable
Example Code:
#include <stdio.h>
int main()
{
int a = 10;
int *iptr1 = &a;
int **iptr2 = &iptr1;
printf("Value of a=%d\n",a); // Value of a=10
printf("Address of a=%d\n",&a); // Address of a= 22 93 532
printf("\n");
printf("Value of iptr1=%d\n",iptr1);// Value of iptr1=22 93 532
printf("Address of iptr1=%d\n",&iptr1);// Address of iptr1= 22 93 528
printf("\n");
printf("Value of iptr2=%d\n",iptr2); // Value of iptr2=22 93 528
printf("Address of iptr2=%d\n",&iptr2);//Address of iptr2=22 93 524
printf("\n");
printf("Value of a=%d\n",a); // Value of a=10
printf("Value of a using iptr1=%d\n",*iptr1);//Value of a using iptr1=10
printf("Value of a using iptr2=%d\n",**iptr2); // Value of a using iptr2=10
printf("\n");
a = 20;
printf("Value of a=%d\n",a); // Value of a=20
printf("Value of a using iptr1=%d\n",*iptr1);//Value of a using iptr1=20
printf("Value of a using iptr2=%d\n",**iptr2); // Value of a using iptr2=20
printf("\n");
*iptr1 = 30;
printf("Value of a=%d\n",a); // Value of a=30
printf("Value of a using iptr1=%d\n",*iptr1);//Value of a using iptr1=30
printf("Value of a using iptr2=%d\n",**iptr2); // Value of a using iptr2=30
printf("\n");
**iptr2 = 40;
printf("Value of a=%d\n",a); // Value of a=40
printf("Value of a using iptr1=%d\n",*iptr1);//Value of a using iptr1=40
printf("Value of a using iptr2=%d\n",**iptr2); // Value of a using iptr2=40
printf("\n");
return 0;
}