구조체는 사용자가 만드는 자료형이다.
#include <stdio.h>
struct student{
int num;
double grade;
};
int main(){
struct student s1;
s1.num=2;
s1.grade=2.7;
printf("학번 : %d\\n",s1.num);
printf("학점 : %.1lf\\n",s1.grade);
return 0;
}
구조체 변수의 크기
패딩바이트를 넣어서 가지런하게 정렬(바이트 얼라인먼트)를 한다.
구조체에 있는 변수중 가장 크기가 큰 변수를 기준으로 패딩비트를 채워넣는다.
구조체포인터와 →연산자
#include <stdio.h>
struct score{
int kor;
int eng;
int math;
};
int main(){
struct score yuni = {90,80,70};
struct score *ps = &yuni;
printf("%d",(*ps).kor);
printf("%d",ps->eng);
printf("%d",ps->math);
return 0;
}
구조체를 가리키는 구조체포인터를 정의하였고,
구조체포인터에서 구조체의 인수를 접근하기 간편한 →연산자이다.
typedef
typedef struct{ // 구조체이름 재정의하므로 생략
int num;
double grade;
}Student;
'SW > C' 카테고리의 다른 글
| 13-2 함수의 데이터 공유 방법 (0) | 2025.03.30 |
|---|---|
| 12-1 문자열과 포인터 (0) | 2025.03.30 |
| 11-2 버퍼를 사용하는 입력함수 (0) | 2025.03.30 |
| 10-1 배열과 포인터 (0) | 2025.03.30 |
| 9-2 포인터 완전정복 (0) | 2025.03.30 |