Cprogramming 简明教程

Chain of Pointers in C

What is Chain of Pointers in C?

C 中的 chain of pointers 是一系列相互指向的指针。一个指针变量可以存储另一个变量的地址,该变量可以是任何类型,包括另一个指针,在这种情况下,它被称为 pointer to pointer

A chain of pointers in C is a series of pointers that point to each other. A pointer variable can store the address of another variable which can be of any type, including another pointer, in which case it is called a pointer to pointer.

当存在多级指针时,就是指针链。从理论上讲,可以链接的级别没有限制,如下面示意图所示:

A chain of pointers is when there are multiple levels of pointers. Theoretically, there is no limit to how many levels the chaining can be done, as shown in the following diagram −

chain of pointers

Declaration of Chain of Pointers

这可以通过以下代码表示:

This can be represented by the following code −

int a = 10;
int *x = &a;
int **y = &x;
int ***z = &y;

在上面的例子中,“x”是一个指向“int”类型的指针,因为符号“int " indicates. To store the address of "x" in "y", it should be a pointer to a pointer to int, i.e., "int * ”。

In the above example, "x" is a pointer to an "int" type, as the notation "int " indicates. To store the address of "x" in "y", it should be a pointer to a pointer to int, i.e., "int *".

类似地,“z”是一个指向指向指向 int 的指针的“指针”,因此它的声明中出现了三个星号,即“int * ”。

Similarly, "z" is a "pointer to a pointer to a pointer" to int, hence the asterisk appears thrice in its declaration, i.e., "int *".

How Does the Dereferencing Work?

我们知道“*x”返回存储在“x”中的地址的值,即“a”的值。

We know that "*x" returns the value at the address stored in "x", i.e., the value of "a".

按照同样的逻辑,“ y" should return its value (refer to the above diagram) which is 1000, which in turn is the address of "a". Hence, the double dereferencing of "y" (i.e., " *y”应该给你“a”的值。

Going by the same logic, "y" should return its value (refer to the above diagram) which is 1000, which in turn is the address of "a". Hence, the double dereferencing of "y" (i.e., "*y") should give you the value of "a".

此外,“z”的三次引用“***z”应该给出“a”的值。

Further, a triple referencing of "z" as "***z" should give the value of "a".

Example

以下示例展示了“double dereferencing”和“triple dereferencing”如何工作的:

The following example shows how "double dereferencing" and "triple dereferencing" work −

#include <stdio.h>

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;
}

请注意,解除引用的所有这三种情况都打印出“a”的值:

Notice that all the three cases of dereferencing print the value of "a" −

a: 10
a: 10
a: 10
a: 10

A Chain of Float Pointers

我们可以遵循相同的逻辑来创建一个浮点指针链,并对多个级别执行解除引用以获得浮点变量的值。

We can follow the same logic to create a chain of float pointers and apply dereferencing of multiple levels to obtain the value of a float variable.

Example

以下示例展示了如何使用浮点指针链:

The following example shows how you can work with a chain of float pointers −

#include <stdio.h>

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;
}

运行代码并检查其输出:

Run the code and check its output −

a: 10.250000
a: 10.250000
a: 10.250000
a: 10.250000

Updating the Original Variable by Dereferencing

我们还可以通过解除引用来更新原始 variable 的值。看一看下面的声明:

We can also update the value of the original variable by dereferencing. Take a look at the following statement −

*x = 11.25;

它将相应地更改“a”的值。类似地,它可以通过后续级别的指针更新。

It will change the value of "a" accordingly. Similarly, it can be updated with the pointer at subsequent levels.

Example

以下程序展示了如何使用不同级别的解除引用来更新原始变量:

The following program shows how you can update the original variable using different levels of dereferencing −

#include <stdio.h>

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;
}

运行代码并检查其输出:

Run the code and check its output −

a:10.250000
a:11.250000
a:12.250000
a:13.250000

A Chain of Character Pointers

string 表示为“char”类型的 array 或指向 “char”类型的指针−

A string is represented as an array of "char" type or a pointer to "char" type −

char *a = "Hello";

因此,我们可以创建一个 char 指针链。

Hence, we can create a chain of char pointers.

Note : 唯一的 difference 是,此处原始变量本身是一个指针,因此高级指针具有 one asterisk more ,与之前的示例相比。

Note: The only difference is, here the original variable itself is a pointer, so the upper-level pointers have one asterisk more, as compared to the earlier examples.

Example

以下示例演示了字符指针链如何工作 −

The following example shows how a chain of character pointers works −

#include <stdio.h>

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;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

a: Hello
a: Hello
a: Hello
a: Hello

指针链接可用于创建 linked lists 和其他 data structures

Chaining of pointers is useful for creating linked lists and other data structures.