ポインタの例を示すCプログラムを作成する
ポインタは、別の変数のアドレスを格納する変数です。
ポインタの機能
-
ポインタはメモリスペースを節約します。
-
メモリ位置に直接アクセスできるため、ポインタの実行時間が短縮されます。
-
ポインタを使用すると、メモリに効率的にアクセスできます。つまり、メモリは動的に割り当てられ、割り当てが解除されます。
-
ポインタはデータ構造で使用されます。
ポインタの宣言
int *p;
これは、「p」が別の整数変数のアドレスを保持するポインタ変数であることを意味します。
ポインタの初期化
アドレス演算子(&)は、ポインタ変数を初期化するために使用されます。
たとえば、
int qty = 175; int *p; p= &qty;
ポインタを介して変数にアクセスする
変数の値にアクセスするには、間接演算子(*)を使用します。
プログラム
#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\n",sizeof (int));//4//
printf("Address of %d is %d\n",a,p);//Address value//
printf("Value of %d is %d\n",a,*p);//2//
printf("Value of next address location of %d is %d\n",a,*(p+1));//Garbage value from (p+1) address//
printf("Address of next address location of %d is %d\n",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\n",sizeof(char));//1//
printf("Address of %d is %d\n",a,p0);//Address Value(p)//
printf("Value of %d is %d\n",a,*p0);//First byte of value a - 2//
printf("Value of next address location of %d is %d\n",a,*(p0+1));//Second byte of value a - 0//
printf("Address of next address location of %d is %d\n",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プログラムを書く
配列は、共通の名前で保存される関連アイテムのグループです。 構文 配列を宣言するための構文は次のとおりです- datatype array_name [size]; 初期化 配列は宣言時に初期化することもできます- int a[5] = { 10,20,30,40,50}; Cの逆配列 スワッピング手法を使用して配列を逆にすることができます。 たとえば、「P」が4つの要素を持つ整数の配列である場合- P[0] = 1, P[1] = 2, P[2] = 3 and P[3]=4 次に、反転した後- P[0] = 4, P[1] = 3, P[2] = 2 and P[3]=1
-
PGM形式で画像を書き込むCプログラム
PGMはポータブルグレーマップです。 2D配列をPNG、JPEG、またはその他の画像形式の画像としてCに保存する場合は、ファイルに書き込む前に、指定された形式でデータをエンコードするために多くの作業を行う必要があります。 Netpbm形式は、簡単でポータブルなソリューションを提供します。 Netpbmはグラフィックプログラムのオープンソースパッケージであり、基本的にLinuxまたはUnixプラットフォームで使用されます。 MicrosoftWindowsシステムでも動作します。 各ファイルは2バイトのマジックナンバーで始まります。このマジックナンバーは、ファイルのタイプを識別するために使用