C 프로그래밍의 구조 배열은 단일 이름으로 함께 그룹화된 다양한 데이터 유형 변수의 모음입니다.
구조 선언의 일반 형식
구조적 선언은 다음과 같습니다 -
struct tagname{
datatype member1;
datatype member2;
datatype member n;
}; 여기서 구조체 키워드입니다.
태그 이름 구조의 이름을 지정합니다.
멤버1, 멤버2 구조를 구성하는 데이터 항목을 지정합니다.
예시
다음 예는 C 프로그래밍의 구조 내에서 배열의 사용법을 보여줍니다 -
struct book{
int pages;
char author [30];
float price;
}; 예시
다음은 구조체 내에서 배열의 사용을 보여주는 C 프로그램입니다 -
#include <stdio.h>
// 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\n", a1.roll_no);
printf("Grade : %c\n", a1.grade);
printf("Marks secured:\n");
int i;
int len = sizeof(a1.marks) / sizeof(float);
// Accessing the contents of the
// array within the structure
for (i = 0; i < len; i++) {
printf("Subject %d : %.2f\n",
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