C 程式顯示指標到指標之間的關係


在 C 程式語言中,指向指標或雙指標是一個儲存另一個指標地址的變數。

宣告

以下是指向指標的宣告 -

datatype ** pointer_name;

例如,int **p;

此處,p 是指向指標的指標。

初始化

‘&’用於初始化。

例如,

int a = 10;
int *p;
int **q;
p = &a;

訪問

解引用運算子 (*) 用於訪問

示例程式

以下是雙指標的 C 程式 -

 線上演示

#include<stdio.h>
main ( ){
   int a = 10;
   int *p;
   int **q;
   p = &a;
   q = &p;
   printf("a =%d ",a);
   printf(" a value through pointer = %d", *p);
   printf(" a value through pointer to pointer = %d", **q);
}

輸出

當執行上述程式時,它生成以下結果 -

a=10
a value through pointer = 10
a value through pointer to pointer = 10

示例

下面,考慮另一個 C 程式,該程式展示指向指標之間的關係。

 線上演示

#include<stdio.h>
void main(){
   //Declaring variables and pointers//
   int a=10;
   int *p;
   p=&a;
   int **q;
   q=&p;
   //Printing required O/p//
   printf("Value of a is %d
",a);//10//    printf("Address location of a is %d
",p);//address of a//    printf("Value of p which is address location of a is %d
",*p);//10//    printf("Address location of p is %d
",q);//address of p//    printf("Value at address location q(which is address location of p) is %d
",*q);//address of a//    printf("Value at address location p(which is address location of a) is %d
",**q);//10// }

輸出

當執行上述程式時,它生成以下結果 -

Value of a is 10
Address location of a is 6422036
Value of p which is address location of a is 10
Address location of p is 6422024
Value at address location q(which is address location of p) is 6422036
Value at address location p(which is address location of a) is 10

更新日期: 2021 年 3 月 11 日

260 次瀏覽

開啟您的 職業生涯

完成課程後獲得認證

開始學習
廣告
© . All rights reserved.