C言語の2次元配列とは何ですか?
配列は、共通の名前で保存される関連アイテムのグループです。
構文
配列を宣言するための構文は次のとおりです-
datatype array_name [size];
アレイの種類
アレイは大きく3つのタイプに分類されます。それらは次のとおりです-
- 1次元配列
- 2次元配列
- 多次元配列
初期化
配列は、次の2つの方法で初期化できます-
- コンパイル時の初期化。
- 実行時の初期化。
2つの多次元配列
これらは、値のテーブルをマトリックスアプリケーションに格納(または)する必要がある状況で使用されます。
構文
構文は以下のとおりです-
datatype array_ name [rowsize] [column size];
例 int a [5] [5];
| a [0] [0] 10 | a [0] [1] 20 | a [0] [2] 30 |
| a [1] [0] 40 | a [1] [1] 50 | a [1] [2] 60 |
| a [2] [0] | a [2] [1] | a [2] [2] |
以下は、コンパイル時の初期化用のCプログラムです。 −
例
#include<stdio.h>
main ( ){
int a[3][3] = {10,20,30,40,50,60,70,80,90};
int i,j;
printf ("elements of the array are");
for ( i=0; i<3; i++){
for (j=0;j<3; j++){
printf("%d \t", a[i] [j]);
}
printf("\n");
}
} 出力
出力は以下のとおりです-
elements of the array are: 10 20 30 40 50 60 70 80 90
以下は、ランタイム初期化用のCプログラムです。 −
例
#include<stdio.h>
main ( ){
int a[3][3] ,i,j;
printf ("enter elements of array");
for ( i=0; i<3; i++){
for (j=0;j<3; j++){
scanf("%d", &a[i] [j]);
}
}
printf("elements of the array are");
for ( i=0; i<3; i++){
for (j=0;j<3; j++){
printf("%d\t", a[i] [j]);
}
printf("\n");
}
} 出力
出力は以下のとおりです-
Enter elements of array : 1 2 3 4 5 6 7 8 9 Elements of the array are 1 2 3 4 5 6 7 8 9
以下に示すのは、実行時コンパイルを使用して配列内のすべての要素の合計と積を計算するCプログラムです-
例
#include<stdio.h>
void main(){
//Declaring the array - run time//
int A[2][3],B[2][3],i,j,sum[i][j],product[i][j];
//Reading elements into the array's A and B using for loop//
printf("Enter elements into the array A: \n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("A[%d][%d] :",i,j);
scanf("%d",&A[i][j]);
}
printf("\n");
}
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("B[%d][%d] :",i,j);
scanf("%d",&B[i][j]);
}
printf("\n");
}
//Calculating sum and printing output//
printf("Sum array is : \n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
sum[i][j]=A[i][j]+B[i][j];
printf("%d\t",sum[i][j]);
}
printf("\n");
}
//Calculating product and printing output//
printf("Product array is : \n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
product[i][j]=A[i][j]*B[i][j];
printf("%d\t",product[i][j]);
}
printf("\n");
}
} 出力
出力は以下のとおりです-
Enter elements into the array A: A[0][0] :2 A[0][1] :3 A[0][2] :1 A[1][0] :2 A[1][1] :4 A[1][2] :5 B[0][0] :1 B[0][1] :2 B[0][2] :3 B[1][0] :5 B[1][1] :6 B[1][2] :7 Sum array is : 3 5 4 7 10 12 Product array is : 2 6 3 10 24 35
-
C言語のインライン関数とは何ですか?
インライン関数は、関数呼び出しが行われている場所で置き換えることができます。関数の置換は常にコンパイラの選択です。 インライン関数では、関数呼び出しは実際のプログラムコードに置き換えられます。 ほとんどのインライン関数は、小さな計算に使用されます。大規模なコンピューティングには適していません。 インライン関数は通常の関数に似ています。唯一の違いは、関数名の前にキーワードをインラインで配置することです。 インライン関数は次の構文で作成されます- inline function_name (){ //function definition } 例
-
C言語でポインタと2次元配列を説明する
ポインタは、別の変数のアドレスを格納する変数です。 機能 ポインタはメモリスペースを節約します。 メモリ位置に直接アクセスできるため、ポインタの実行時間が短縮されます。 ポインタを使用すると、メモリに効率的にアクセスできます。つまり、メモリは動的に割り当てられ、割り当てが解除されます。 ポインタはデータ構造で使用されます。 ポインタと2次元配列 2次元配列のメモリ割り当ては次のとおりです- int a[3] [3] = {1,2,3,4,5,6,7,8,9}; a[1] [2] = *(1234 + 1*3+2) = *(1234 + 3+2) = *