Understanding Chain of Pointers in C
This resource explores the concept of chain of pointers in C programming. It explains how multiple levels of pointers can be linked together to create complex data structures and provides practical examples of their usage.
Chain of Pointers in C
What is Chain of Pointers in C?
A chain of pointers in C refers to a series of pointers that point to one another. A pointer variable can store the address of another variable, which can also be a pointer. This scenario leads to the concept of a pointer to a pointer.
A chain of pointers allows multiple levels of pointers. Theoretically, there's no limit to how many levels can be chained together, as illustrated in the following diagram:
Declaration of Chain of Pointers
Here’s how you can declare a chain of pointers:
Code Example
int a = 10;
int *x = &a;
int **y = &x;
int ***z = &y;
In this example:
x
is a pointer to anint
type, denoted byint *
.y
is a pointer to a pointer to anint
, represented asint **
.z
is a pointer to a pointer to a pointer to anint
, represented asint ***
.
How Does the Dereferencing Work?
Dereferencing works as follows:
*x
returns the value at the address stored inx
, which is the value ofa
.*y
returns the address stored iny
, which leads toa
. Therefore, double dereferencing**y
gives the value ofa
.- Similarly, triple dereferencing
***z
also results in the value ofa
.
Example: Double and Triple Dereferencing
The following example demonstrates how double and triple dereferencing work:
Code Example
#include
int main() {
int a = 10;
int *x = &a;
int **y = &x;
int ***z = &y;
printf("a: %d\n", a);
printf("a: %d\n", *x); // dereferencing x
printf("a: %d\n", **y); // double dereferencing y
printf("a: %d\n", ***z); // triple dereferencing z
return 0;
}
Output
a: 10
a: 10
a: 10
a: 10
A Chain of Float Pointers
You can apply the same concept to create a chain of float pointers. Here’s an example:
Code Example
#include
int main() {
float a = 10.25;
float *x = &a;
float **y = &x;
float ***z = &y;
printf("a: %f\n", a);
printf("a: %f\n", *x);
printf("a: %f\n", **y);
printf("a: %f\n", ***z);
return 0;
}
Output
a: 10.250000
a: 10.250000
a: 10.250000
a: 10.250000
Updating the Original Variable by Dereferencing
You can update the value of the original variable by dereferencing. For example:
Code Example
#include
int main() {
float a = 10.25;
float *x = &a;
float **y = &x;
float ***z = &y;
printf("a: %f\n", a);
// update with first pointer
*x = 11.25;
printf("a: %f\n", *x);
// update with second pointer
**y = 12.25;
printf("a: %f\n", **y);
// update with third pointer
***z = 13.25;
printf("a: %f\n", ***z);
return 0;
}
Output
a: 10.250000
a: 11.250000
a: 12.250000
a: 13.250000
A Chain of Character Pointers
A string is represented as an array of char
type or a pointer to char
type:
Code Example
char *a = "Hello";
Thus, we can create a chain of char pointers. The only difference here is that the original variable is a pointer, which requires an additional asterisk in the upper-level pointers compared to the previous examples.
Example: Chain of Character Pointers
Here’s how a chain of character pointers works:
Code Example
#include
int main() {
char *a = "Hello";
char **x = &a;
char ***y = &x;
char ****z = &y;
printf("a: %s\n", a);
printf("a: %s\n", *x);
printf("a: %s\n", **y);
printf("a: %s\n", ***z);
return 0;
}
Output
a: Hello
a: Hello
a: Hello
a: Hello
Conclusion
Chaining pointers is useful for creating linked lists and other data structures, enhancing the flexibility and functionality of programs in C.