2학년 2학기/c언어
11-1장 구조체
kkkkk1023
2024. 11. 11. 10:40
구조체의 선언
struct student { //구조체 정의
int number;
char name[10];
double grade;
};
int main(void){
struct student s1; //구조체 변수 선언
}
구조체의 초기화
struct student {
int number;
char name[10];
double grade;
};
int main(){
struct student s1 = { 24, "Kim", 4.3 }; // (1) 구조체 기본 초기화
struct student s2 = s1; // (2) 이미 초기호된 구조체를 할당받아서 초기화
}
구조체 멤버 참조
위의 방식처럼 한번에 초기화하는 방법도 있지만 메모 참조(.)를 이용해서 초기화하는 것도 가능하다.
s1.number = 20170001; // 정수 멤버
strcpy(s1.name, "Kim"); // 문자열 멤버
s1.grade = 4.3; // 실수 멤버
. 문법을 이용해서 구조체의 멤버에 접근해서 값을 할당하거나 출력하는 것이 가능하다.