📜  C中的指针链与示例

📅  最后修改于: 2021-05-25 19:47:03             🧑  作者: Mango

先决条件: C中的指针,C中的双指针(指向指针的指针)

指针用于指向变量的存储位置。指针存储变量的地址。

类似地,当存在多个级别的指针时,就是一个指针链。简化来说,指针指向变量的地址,双指针指向变量,依此类推。这称为多重间接寻址

句法:

// level-1 pointer declaration
datatype *pointer; 

// level-2 pointer declaration
datatype **pointer; 

// level-3 pointer declaration
datatype ***pointer; 
.
.
and so on

指针的级别取决于在声明时指针变量前面有多少个星号。

宣言:

int *pointer_1;
int **pointer_2;
int ***pointer_3;
.
.
and so on

根据内存大小,指针或说链的级别可以上升到N级。如果要创建5级指针,则在声明时需要在指针变量名前加5个星号(*)。

初始化:

// initializing level-1 pointer
// with address of variable 'var'
pointer_1 = &var;

// initializing level-2 pointer
// with address of level-1 pointer
pointer_2 = &pointer_1;

// initializing level-3 pointer
// with address of level-2 pointer
pointer_3 = &pointer_2;
.
.
and so on

范例1:

如图所示,变量“ a”是一个普通的整数变量,其存储整数值10并位于位置2006。“ ptr1”是一个指针变量,它指向整数变量“ a”并存储其位置(即2006)作为其变量。价值。同样,ptr2指向指针变量ptr1,而ptr3指向指针变量ptr2。由于每个指针都直接或间接指向变量“ a”,因此它们都具有与变量“ a”相同的整数值,即10

让我们用下面给出的代码更好地理解:

#include 
// C program for chain of pointer
  
int main()
{
    int var = 10;
  
    // Pointer level-1
    // Declaring pointer to variable var
    int* ptr1;
  
    // Pointer level-2
    // Declaring pointer to pointer variable *ptr1
    int** ptr2;
  
    // Pointer level-3
    // Declaring pointer to double pointer **ptr2
    int*** ptr3;
  
    // Storing address of variable var
    // to pointer variable ptr1
    ptr1 = &var;
  
    // Storing address of pointer variable
    // ptr1 to level -2 pointer ptr2
    ptr2 = &ptr1;
  
    // Storing address of level-2 pointer
    // ptr2 to level-3 pointer ptr3
    ptr3 = &ptr2;
  
    // Displaying values
    printf("Value of variable "
           "var = %d\n",
           var);
    printf("Value of variable var using"
           " pointer ptr1 = %d\n",
           *ptr1);
    printf("Value of variable var using"
           " pointer ptr2 = %d\n",
           **ptr2);
    printf("Value of variable var using"
           " pointer ptr3 = %d\n",
           ***ptr3);
  
    return 0;
}
输出:
Value of variable var = 10
Value of variable var using pointer ptr1 = 10
Value of variable var using pointer ptr2 = 10
Value of variable var using pointer ptr3 = 10

示例2:考虑下面给出的代码,在该代码中我们采用了变量的float数据类型,因此现在对于指针链我们也必须采用相同的数据类型。作为指针和变量,它所指向的应该具有相同的数据类型。

#include 
int main()
{
    float var = 23.564327;
  
    // Declaring pointer variables upto level_4
    float *ptr1, **ptr2, ***ptr3, ****ptr4;
  
    // Iinitializing pointer variables
    ptr1 = &var;
    ptr2 = &ptr1;
    ptr3 = &ptr2;
    ptr4 = &ptr3;
  
    // Printing values
    printf("Value of var = %f\n", var);
    printf("Value of var using level-1"
           " pointer = %f\n",
           *ptr1);
    printf("Value of var using level-2"
           " pointer = %f\n",
           **ptr2);
    printf("Value of var using level-3"
           " pointer = %f\n",
           ***ptr3);
    printf("Value of var using level-4"
           " pointer = %f\n",
           ****ptr4);
  
    return 0;
}
输出:
Value of var = 23.564327
Value of var using level-1 pointer = 23.564327
Value of var using level-2 pointer = 23.564327
Value of var using level-3 pointer = 23.564327
Value of var using level-4 pointer = 23.564327

示例3:使用链接指针更新变量
正如我们已经知道的那样,指针指向变量的地址位置,因此当我们访问指针的值时,它将指向变量的值。现在要更新变量的值,我们可以使用任何级别的指针,因为最终每个指针仅直接或间接指向该变量。它会直接更改存在于变量地址位置的值。

#include 
  
int main()
{
    // Initializing integer variable
    int var = 10;
  
    // Declaring pointer variables upto level-3
    int *ptr1, **ptr2, ***ptr3;
  
    // Iinitializing pointer variables
    ptr1 = &var;
    ptr2 = &ptr1;
    ptr3 = &ptr2;
  
    // Printing values BEFORE updation
    printf("Before:\n");
    printf("Value of var = %d\n", var);
    printf("Value of var using level-1"
           " pointer = %d\n",
           *ptr1);
    printf("Value of var using level-2"
           " pointer = %d\n",
           **ptr2);
    printf("Value of var using level-3"
           " pointer = %d\n",
           ***ptr3);
  
    // Updating var's value using level-3 pointer
    ***ptr3 = 35;
  
    // Printing values AFTER updation
    printf("After:\n");
    printf("Value of var = %d\n", var);
    printf("Value of var using level-1"
           " pointer = %d\n",
           *ptr1);
    printf("Value of var using level-2"
           " pointer = %d\n",
           **ptr2);
    printf("Value of var using level-3"
           " pointer = %d\n",
           ***ptr3);
  
    return 0;
}
输出:
Before:
Value of var = 10
Value of var using level-1 pointer = 10
Value of var using level-2 pointer = 10
Value of var using level-3 pointer = 10
After:
Value of var = 35
Value of var using level-1 pointer = 35
Value of var using level-2 pointer = 35
Value of var using level-3 pointer = 35

注意: Level-N指针只能用于指向level-(N-1)指针。除了Level-1指针。 1级指针将始终指向变量。

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。