C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Pointers Chaining

C Programming

In this tutorial we will learn to chain pointer variables in C programming language.

In the previous tutorial Pointers and Variables we learned how to manipulate data stored in a variable using pointer variable.

Now we will be chaining some more pointer variables to manipulate other variables.

Note! Complexity increases as we chain more pointers.

Creating an integer variable

We will start by creating an integer variable num and assign value 10 to it.

int num = 10;

So, three things happens on writing the above code.

  • A memory location is allocated to store the integer variable.
  • The value 10 is stored in the memory location.
  • The memory location is referred by the variable name num.

In the above image it is shown that the integer variable num gets the memory location 8280 and in that location the value 10 is stored.

Creating integer pointer variable for the integer variable

Now we will create an integer pointer variable ptr that will point at the integer variable num.

For this we have to save the address of the num variable in the ptr variable.

To get the address of a variable we use the address of & operator.

// integer variable num
int num = 10;

// pointer variable pointing at num
int *ptr = #

In the above image it is shown that the integer pointer variable ptr gets the memory location 8272 and in that location the address of the num variable i.e., 8280 is stored. So, ptr variable is pointing at the num variable.

Creating the second integer pointer variable for the first integer pointer variable

To create a second pointer variable to point at the first pointer variable we use the following syntax.

dataType **secondPtr = &firstPtr;

In the following example we are creating the second integer pointer variable ptr2 that points at the first integer pointer variable ptr which is pointing at the integer variable num.

int **ptr2 = &ptr;

In the above image it is shown that the second integer pointer variable ptr2 gets the memory location 8264 and in that location the address of the first pointer variable ptr i.e., 8272 is stored. So, ptr2 variable is pointing at the ptr variable.

Update value of integer variable via second integer pointer variable

For this we have to first get the address of the variable num which is stored in integer pointer variable ptr.

We use the value at the address of * operator to get the value stored at a given memory address.

To get the address of the integer variable num which is stored as a value in integer pointer variable ptr we have to write the following.

*ptr

And to get the address of the integer pointer variable ptr which is stored as a value in integer pointer variable ptr2 we have to write the following.

*(*ptr2)

So, *ptr2 is pointing at ptr and *ptr is pointing at num.

And *(*ptr2) is giving us the value stored at the memory address (in this case 8280) that is allocated to the num variable.

So, to update the value stored in the integer variable num via the second integer pointer variable ptr2 we have to write the following.

// updating the value of num via ptr2
*(*ptr2) = 20;

Complete code

#include <stdio.h>

int main(void) {
  
  // num variable
  int num = 10;

  // ptr pointer variable
  int *ptr = NULL;
  
  // second ptr2 pointer variable
  int **ptr2 = NULL;

  // assigning the address of num to ptr
  ptr = &num;
  
  // assigning the address of ptr to ptr2
  ptr2 = &ptr;

  // printing the value of num - Output: 10
  printf("num: %d\n", num);
  printf("num via ptr: %d\n", *ptr);
  printf("num via ptr2: %d\n", *(*ptr2));
 
  // updating the value of num via ptr2
  printf("Updating value of num via ptr2...\n");
  *(*ptr2) = 20;

  // printing the new value of num - Output: 20
  printf("num: %d\n", num);
  printf("num via ptr: %d\n", *ptr);
  printf("num via ptr2: %d\n", *(*ptr2));

  return 0;
}

Output:

num: 10
num via ptr: 10
num via ptr2: 10
Updating value of num via ptr2...
num: 20
num via ptr: 20
num via ptr2: 20

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT