首頁  >  文章  >  後端開發  >  C程式以顯示指向指標之間的關係

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

PHPz
PHPz轉載
2023-09-08 23:45:021399瀏覽

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</p><p>",a);//10//
   printf("Address location of a is %d</p><p>",p);//address of a//
   printf("Value of p which is address location of a is %d</p><p>",*p);//10//
   printf("Address location of p is %d</p><p>",q);//address of p//
   printf("Value at address location q(which is address location of p) is %d</p><p>",*q);//address of a//
   printf("Value at address location p(which is address location of a) is %d</p><p>",**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

以上是C程式以顯示指向指標之間的關係的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除