[C++]코딩연습장
C++ [구조체]
KAU
2021. 2. 21. 15:14
구조체 선언과 초기화
멤버 접근
#include <iostream>
using namespace std;
int main() {
//구조체 : 다른 데이터형이 허용되는 데이터의 집합
//cf) 배열 : 같은 데이터형의 집합
//야구선수
struct MyStruct
{
string name;
string position;
float height;
float weight;
//멤버라고 불리는 구성요소가 자리잡은것이다.
};
MyStruct A; //MyStruct 형의 변수를 선언하게 되는 것이다.
A.name = "chan gue";
A.position ="Pitcher";
A.height = 183;
A.weight = 77;
MyStruct B = {
"chan gue",
"Pitcher",
183,
77
}; //초기화도 가능하다.
cout << A.name << endl;
cout << A.position << endl;
cout << A.height << endl;
cout << A.weight << endl;
return 0;
}
MyStruct C[2] = {
{"A","B",1,1},
{"B","B",2,2}
};
cout << C[0].height << endl;
구조체도 배열로 선언 가능하다.
구조체 초기화
#include <iostream>
using namespace std;
int main() {
struct MyStruct
{
string name;
string position;
float height;
float weight;
//멤버라고 불리는 구성요소가 자리잡은것이다.
} D;
D = {
};
cout << D.height << endl;
return 0;
}