구조 단일 이름으로 함께 그룹화된 다양한 데이터 유형 변수의 모음입니다.
구조의 특징
C 프로그래밍 언어에서 구조의 기능은 다음과 같습니다 -
-
할당 연산자를 사용하여 다른 데이터 유형의 모든 구조 요소의 내용을 해당 유형의 다른 구조 변수에 복사할 수 있습니다.
-
복잡한 데이터 유형을 처리하려면 중첩 구조라고 하는 다른 구조 내에 구조를 만드는 것이 좋습니다.
-
전체 구조, 구조의 개별 요소 및 구조 주소를 함수에 전달할 수 있습니다.
-
구조체 포인터를 생성할 수 있습니다.
구조의 선언 및 초기화.
구조 선언의 일반적인 형식은 다음과 같습니다 -
datatype member1; struct tagname{ datatype member2; datatype member n; };
여기,
- 구조체 는 키워드입니다.
- 태그 이름 구조의 이름을 지정합니다.
- 구성원1, 구성원2 데이터 항목입니다.
예:
struct book{ int pages; char author [30]; float price; };
프로그램
다음은 구조를 사용하여 이름을 알파벳순으로 정렬하는 C 프로그램입니다. -
#include<stdio.h> #include<string.h> struct tag{ char name[10]; int rno; }; typedef struct tag node; node s[5]; sort(int no){ int i,j; node temp; for(i=0;i<no-1;i++) for(j=i+1;j<no;j++) if(strcmp(s[i].name,s[j].name)>0){ temp=s[i]; s[i]=s[j]; s[j]=temp; } } void main(){ int no,i; fflush(stdin); printf("Enter The Number Of Students:"); scanf("%d",&no); for(i=0;i<no;i++){ printf("Enter The Name:"); fflush(stdin); gets(s[i].name); printf("Enter the Roll:"); scanf("%d",&s[i].rno); } sort(no); for(i=0;i<no;i++){ printf("%s\t",s[i].name); printf("%d\n",s[i].rno); } }
출력
위의 프로그램을 실행하면 다음과 같은 결과가 나온다 -
Enter The Number of Students:5 Enter The Name:Priya Enter the Roll:3 Enter The Name:Hari Enter the Roll:5 Enter The Name:Pinky Enter the Roll:7 Enter The Name:Lucky Enter the Roll:1 Enter The Name:Krishna Enter the Roll:2 Hari 5 Krishna 2 Lucky 1 Pinky 7 Priya 3