ホームページ  >  記事  >  バックエンド開発  >  ポインタ間の関係を示す 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

Example

ここで、ポインター間の関係を示す別の 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。