ホームページ  >  記事  >  バックエンド開発  >  ポインターの例を示す C プログラムを作成します。

ポインターの例を示す C プログラムを作成します。

WBOY
WBOY転載
2023-09-21 19:33:02976ブラウズ

ポインタは、別の変数のアドレスを格納する変数です。

ポインタの特性

  • ポインタはメモリ領域を節約します。

  • メモリ位置に直接アクセスできるため、ポインターの実行時間が短縮されます。

  • ポインタを使用すると、メモリに効率的にアクセスできます。つまり、メモリは動的に割り当てられ、解放されます。

  • ポインタはデータ構造で使用されます。

ポインタの宣言

int *p;

これは、「p」が別の整変数のアドレスを保持するポインタ変数であることを意味します。

ポインターの初期化

アドレス演算子 (&) は、ポインター変数の初期化に使用されます。

例:

int qty = 175;
int *p;
p= &qty;

ポインターの例を示す C プログラムを作成します。

変数を介した変数ポインターへのアクセス

変数の値にアクセスするには、間接演算子を使用します。 (*)。

プログラム

ライブデモンストレーション

#include<stdio.h>
void main(){
   //Declaring variables and pointer//
   int a=2;
   int *p;
   //Declaring relation between variable and pointer//
   p=&a;
   //Printing required example statements//
   printf("Size of the integer is %d</p><p>",sizeof (int));//4//
   printf("Address of %d is %d</p><p>",a,p);//Address value//
   printf("Value of %d is %d</p><p>",a,*p);//2//
   printf("Value of next address location of %d is %d</p><p>",a,*(p+1));//Garbage value from (p+1) address//
   printf("Address of next address location of %d is %d</p><p>",a,(p+1));//Address value +4//
   //Typecasting the pointer//
   //Initializing and declaring character data type//
   //a=2 = 00000000 00000000 00000000 00000010//
   char *p0;
   p0=(char*)p;
   //Printing required statements//
   printf("Size of the character is %d</p><p>",sizeof(char));//1//
   printf("Address of %d is %d</p><p>",a,p0);//Address Value(p)//
   printf("Value of %d is %d</p><p>",a,*p0);//First byte of value a - 2//
   printf("Value of next address location of %d is %d</p><p>",a,*(p0+1));//Second byte of value a - 0//
   printf("Address of next address location of %d is %d</p><p>",a,(p0+1));//Address value(p)+1//
}

出力

Size of the integer is 4
Address of 2 is 6422028
Value of 2 is 2
Value of next address location of 2 is 10818512
Address of next address location of 2 is 6422032
Size of the character is 1
Address of 2 is 6422028
Value of 2 is 2
Value of next address location of 2 is 0
Address of next address location of 2 is 6422029

以上がポインターの例を示す C プログラムを作成します。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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