📜  在C / C++中我们可以拥有多少级指针

📅  最后修改于: 2021-05-30 11:50:58             🧑  作者: Mango

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

指针用于指向变量的存储位置。指针存储变量的地址,并且可以使用指针的解引用来访问变量的值。
指针通常初始化为:

datatype *variable name;

上面的声明是单个指针,但是可以有更多的指针。这称为指针级别。根据ANSI C,每个编译器必须至少具有12个指针级别。这意味着我们可以使用12 *符号和变量名。

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

句法:

// 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

下面是说明各个级别的指针的程序:

程序1:

C
// C program to illustrate levels of pointer
#include 
  
// Driver Code
int main()
{
    int var = 10;
  
    // Pointer level-1
    // Declaring pointer to  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 var
    // to pointer variable ptr1
    ptr1 = &var;
  
    // Storing address of pointer
    // ptr1 to level-2 ptr2
    ptr2 = &ptr1;
  
    // Storing address of level-2
    // 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;
}


C++
// C++ program to illustrate
// levels of pointer
#include 
using namespace std;
  
// Driver Code
int main()
{
    int var = 10;
  
    // Pointer level-1
    // Declaring pointer to  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 var
    // to pointer variable ptr1
    ptr1 = &var;
  
    // Storing address of pointer
    // ptr1 to level-2 ptr2
    ptr2 = &ptr1;
  
    // Storing address of level-2
    // ptr2 to level-3 pointer ptr3
    ptr3 = &ptr2;
  
    // Displaying values
    cout << "Value of variable var is "
         << var << endl;
  
    cout << "Value of variable var "
         << "using pointer ptr1 is "
         << *ptr1 << endl;
  
    cout << "Value of variable var "
         << "using pointer ptr2 is "
         << **ptr2 << endl;
  
    cout << "Value of variable var "
         << "using pointer ptr3 is "
         << ***ptr3 << endl;
  
    return 0;
}


C
// C program to illustrate
// levels of pointer
#include 
  
// Driver Code
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;
}


C++
// C++ program to illustrate
// levels of pointer
#include 
using namespace std;
  
// Driver Code
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
    cout << "Value of var is "
         << var << endl;
  
    cout << "Value of var using level-1"
         << " pointer is "
         << *ptr1 << endl;
  
    cout << "Value of var using level-2"
         << " pointer is "
         << **ptr2 << endl;
  
    cout << "Value of var using level-3"
         << " pointer is "
         << ***ptr3 << endl;
  
    cout << "Value of var using level-4"
         << " pointer is "
         << ****ptr4 << endl;
  
    return 0;
}


C
// C program to illustrate
// levels of pointer
#include 
  
// Driver Code
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;
}


C++
// C++ program to illustrate
// levels of pointer
#include 
using namespace std;
  
// Driver Code
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
    cout << "Before:" << endl;
  
    cout << "Value of var is " << var
         << endl;
  
    cout << "Value of var using level-1"
         << " pointer is "
         << *ptr1 << endl;
  
    cout << "Value of var using level-2"
         << " pointer is "
         << **ptr2 << endl;
  
    cout << "Value of var using level-3"
         << " pointer is "
         << ***ptr3 << endl;
  
    // Updating var's value using
    // level-3 pointer
    ***ptr3 = 35;
  
    // Printing values AFTER updation
    cout << "After:" << endl;
  
    cout << "Value of var is " << var
         << endl;
  
    cout << "Value of var using level-1"
         << " pointer is "
         << *ptr1 << endl;
  
    cout << "Value of var using level-2"
         << " pointer is "
         << **ptr2 << endl;
  
    cout << "Value of var using level-3"
         << " pointer is "
         << ***ptr3 << endl;
  
    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:

C

// C program to illustrate
// levels of pointer
#include 
  
// Driver Code
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;
}

C++

// C++ program to illustrate
// levels of pointer
#include 
using namespace std;
  
// Driver Code
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
    cout << "Value of var is "
         << var << endl;
  
    cout << "Value of var using level-1"
         << " pointer is "
         << *ptr1 << endl;
  
    cout << "Value of var using level-2"
         << " pointer is "
         << **ptr2 << endl;
  
    cout << "Value of var using level-3"
         << " pointer is "
         << ***ptr3 << endl;
  
    cout << "Value of var using level-4"
         << " pointer is "
         << ****ptr4 << endl;
  
    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:

C

// C program to illustrate
// levels of pointer
#include 
  
// Driver Code
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;
}

C++

// C++ program to illustrate
// levels of pointer
#include 
using namespace std;
  
// Driver Code
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
    cout << "Before:" << endl;
  
    cout << "Value of var is " << var
         << endl;
  
    cout << "Value of var using level-1"
         << " pointer is "
         << *ptr1 << endl;
  
    cout << "Value of var using level-2"
         << " pointer is "
         << **ptr2 << endl;
  
    cout << "Value of var using level-3"
         << " pointer is "
         << ***ptr3 << endl;
  
    // Updating var's value using
    // level-3 pointer
    ***ptr3 = 35;
  
    // Printing values AFTER updation
    cout << "After:" << endl;
  
    cout << "Value of var is " << var
         << endl;
  
    cout << "Value of var using level-1"
         << " pointer is "
         << *ptr1 << endl;
  
    cout << "Value of var using level-2"
         << " pointer is "
         << **ptr2 << endl;
  
    cout << "Value of var using level-3"
         << " pointer is "
         << ***ptr3 << endl;
  
    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

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

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