両方 構造体の配列 そして 構造内の配列 C プログラミングでは配列と構造体の組み合わせですが、どちらも異なる目的で使用されます。
構造内の配列
構造体はデータ型です。 C これにより、関連する変数のグループを個別のエンティティではなく単一のユニットとして扱うことができます。構造体には、int、char、float、double などのさまざまなデータ型の要素が含まれる場合があります。また、そのメンバーとして配列が含まれる場合もあります。このような配列を構造内配列と呼びます。構造体内の配列は構造体のメンバーであり、構造体の他の要素にアクセスするのと同じようにアクセスできます。
以下は、構造体内で配列の概念を使用するプログラムのデモンストレーションです。プログラムは、以下を含む学生の記録を表示します。 ロール番号 、 学年 、 そして マーク さまざまな科目で確保されています。さまざまな科目のマークは、という配列の下に保存されています。 マーク 。レコード全体は、と呼ばれる構造の下に保存されます。 ある 候補者 。
例
以下のプログラムは、構造体内での配列の使用を示しています。
C
// C program to demonstrate the> // use of an array within a structure> #include> // Declaration of the structure candidate> struct> candidate {> >int> roll_no;> >char> grade;> >// Array within the structure> >float> marks[4];> };> // Function to displays the content of> // the structure variables> void> display(>struct> candidate a1)> {> >printf>(>'Roll number : %d
'>, a1.roll_no);> >printf>(>'Grade : %c
'>, a1.grade);> >printf>(>'Marks secured:
'>);> >int> i;> >int> len =>sizeof>(a1.marks) />sizeof>(>float>);> >// Accessing the contents of the> >// array within the structure> >for> (i = 0; i printf('Subject %d : %.2f
', i + 1, a1.marks[i]); } } // Driver Code int main() { // Initialize a structure struct candidate A = { 1, 'A', { 98.5, 77, 89, 78.5 } }; // Function to display structure display(A); return 0; }> |
>
>出力
Roll number : 1 Grade : A Marks secured: Subject 1 : 98.50 Subject 2 : 77.00 Subject 3 : 89.00 Subject 4 : 78.50>
構造体の配列
配列は、同じタイプのデータ項目のコレクションです。配列の各要素は、int、char、float、double、または構造体にすることができます。構造によって、さまざまなデータ型の要素を 1 つの名前の下にグループ化できることがわかりました。この構造は、それ自体が新しいデータ型であると考えることができます。したがって、配列はこの新しいデータ型の要素で構成できます。構造の配列は、レコードをグループ化する際に応用され、高速アクセスを提供します。
以下は、一連の構造のデモンストレーションです。配列には、クラス内の生徒の詳細が保持されます。詳細には次のものが含まれます。 ロール番号、グレード 、 そしてマーク 、構造 (レコード) の下にグループ化されています。学生ごとに 1 つのレコードが存在します。このようにして、関連する変数のコレクションを 1 つのエンティティの下にまとめて、コードの明瞭性を高め、効率を高めることができます。
例
以下のプログラムは、構造体の配列の使用法を示しています。
C
abcと数字
// C program to demonstrate the> // usage of an array of structures> #include> // Declaring a structure class> struct> class> {> >int> roll_no;> >char> grade;> >float> marks;> };> // Function to displays the contents> // of the array of structures> void> display(>struct> class> class_record[3])> {> >int> i, len = 3;> >// Display the contents of the array> >// of structures here, each element> >// of the array is a structure of class> >for> (i = 0; i printf('Roll number : %d
', class_record[i].roll_no); printf('Grade : %c
', class_record[i].grade); printf('Average marks : %.2f
', class_record[i].marks); printf('
'); } } // Driver Code int main() { // Initialize of an array of structures struct class class_record[3] = { { 1, 'A', 89.5f }, { 2, 'C', 67.5f }, { 3, 'B', 70.5f } }; // Function Call to display // the class_record display(class_record); return 0; }> |
>
>出力
Roll number : 1 Grade : A Average marks : 89.50 Roll number : 2 Grade : C Average marks : 67.50 Roll number : 3 Grade : B Average marks : 70.50>
構造体の配列と構造体内の配列の違い
以下は、構造内の配列と構造の配列の違いを表に示したものです。
| パラメータ | 構造内の配列 | 構造体の配列 |
|---|---|---|
| 基本的な考え方 | 構造体には、メンバー変数として配列が含まれます。 | 各要素が構造体型である配列。 |
| 構文 | 構造体クラス { int ar[10]; a1、a2、a3; | struct クラス { int a, b, c; } 学生[10]; |
| アクセス | 構造体の他の要素にアクセスするのと同じように、ドット演算子を使用してアクセスできます。 | 配列にアクセスするのと同じように、インデックスを作成することでアクセスできます。 |
| 要素の構文にアクセスする | 構造体.配列[インデックス] | 配列[インデックス].メンバー |
| メモリ構造 | 構造内の配列はシーケンシャル メモリに格納され、構造のパディングは配列のサイズに依存しません。 | 構造体のパディングにより、構造体要素の間には空きスペースが生じます。 |