{
struct Person
{
float height;
float weight;
char name[10];
short grade;
};
Person person;
person.height = 174.2f;
person.weight = 67.8f;
person.grade = 1;
strcpy_s(person.name, 10, "David");
cout << person.height << endl;
cout << person.weight << endl;
cout << person.grade << endl;
cout << person.name << endl;
}
struct Person을 선언합니다. 구조체는 height, weight, name, grade 네 개의 멤버를 가집니다.Person person;을 선언하고 각 멤버를 초기화합니다.strcpy_s(person.name, 10, "David");를 사용하여 name 멤버에 문자열을 복사합니다.{
Person person{ 174.2f,67.8f, "David", 1 };
cout << person.height << endl;
cout << person.weight << endl;
cout << person.grade << endl;
cout << person.name << endl;
}
Person person{ 174.2f,67.8f, "David", 1 };를 선언하고 초기화합니다.{
// 이름으로 초기화. C++ 20 이상
Person person{ .height = 174.2f, .weight= 67.8f, .name= "David", .grade=1 };
cout << person.height << endl;
cout << person.weight << endl;
cout << person.grade << endl;
cout << person.name << endl;
}
Person person{ .height = 174.2f, .weight= 67.8f, .name= "David", .grade=1 };를 선언하고 초기화합니다.{
struct EyeSight
{
float left;
float right;
};
struct Person
{
float height;
float weight;
char name[10];
short grade;
EyeSight eyeSight;
};
Person person{ 174.2f, 67.3f, "David", 1, {1.1f, 1.0f} };
cout << person.height << endl;
cout << person.weight << endl;
cout << person.grade << endl;
cout << person.name << endl;
cout << person.eyeSight.left << endl;
cout << person.eyeSight.right << endl;
// 배열이 내부에 있어도 복사가 됨
Person person1 = person;
// 비교는 안 됨
// person == person1;
}
struct EyeSight를 선언합니다.struct Person에 EyeSight를 멤버로 포함시킵니다.Person person{ 174.2f, 67.3f, "David", 1, {1.1f, 1.0f} };를 선언하고 초기화합니다.Person person1 = person;를 사용하여 구조체를 복사합니다.{
Person persons[5];
persons[0].grade = 1;
cout << persons[0].grade << endl; // 1
Person person1 = persons[1]; // person1 은 persons[1]의 복사본이다.
person1.grade = 5; // 복사본의 grade가 변경
cout << person1.grade << endl; // 5
cout << persons[1].grade << endl; // 정의 되지 않은 행동
Person &person2 = persons[2]; // 복사본이 아닌 레퍼런스
person2.grade = 5;
cout << person2.grade << endl; // 5
cout << persons[2].grade << endl; // 5
}
Person persons[5];를 선언하여 구조체 배열을 생성합니다.persons[0].grade를 1로 설정하고 출력합니다.Person person1 = persons[1];를 사용하여 배열의 두 번째 요소를 복사합니다. 복사본의 grade를 5로 변경하고 출력합니다.Person &person2 = persons[2];를 사용하여 배열의 세 번째 요소에 대한 참조를 만듭니다. 참조를 통해 grade를 5로 변경하고 출력합니다.{
// struct 의 사이즈
struct Person0
{
float height; // 4
float weight; // 4
char name[10]; // 10
short grade; // 2
};
cout << sizeof(Person0) << endl;
// height, weight, name, grade
// 4 , 4 , 10 , 2 + (2) 패딩 = 24
struct Person1
{
float height; // 4
short grade; // 2
float weight; // 4
char name[10]; // 10
};
cout << sizeof(Person1) << endl;
// height, grade, weight, name
// 4 , 2(2), 4 , 10 (2) = 24
struct alignas(16) Person2
{
float height; // 4
short grade; // 2
float weight; // 4
char name[10];
};
cout << sizeof(Person2) << endl; // 16의 배수로 정렬
cout << alignof(Person0) << endl;
cout << alignof(Person1) << endl;
cout << alignof(Person2) << endl;
}
struct Person0를 선언하고 크기를 출력합니다.sizeof(Person0)는 24입니다. (height와 weight는 4바이트, name은 10바이트, grade는 2바이트이며, 패딩 2바이트가 추가됩니다.)struct Person1를 선언하고 크기를 출력합니다.sizeof(Person1)는 24입니다. (height는 4바이트, grade는 2바이트, weight는 4바이트, name은 10바이트, 패딩 2바이트가 추가됩니다.)struct alignas(16) Person2를 선언하고 정렬 크기를 16으로 설정합니다. 크기를 출력합니다.sizeof(Person2)는 32입니다. 16바이트 정렬이 적용되므로 16의 배수로 정렬됩니다.alignof를 사용하여 출력합니다.구조체는 여러 데이터를 하나로 묶어서 관리할 수 있는 유용한 데이터 구조입니다. 구조체의 멤버를 초기화하는 방법, 구조체 배열, 구조체 복사 및 참조, 그리고 구조체의 메모리 정렬에 대해 이해해야 합니다. 메모리 정렬은 패딩 바이트로 인해 구조체의 실제 크기와 차이가 있을 수 있습니다. C++20 이상에서는 구조체 멤버를 이름으로 초기화할 수 있으며, 정렬을 위한 alignas 키워드를 사용하여 특정 정렬 크기를 지정할 수 있습니다.