Code & Framework/C++

[C++] 구조체

이성훈 Ethan 2024. 3. 26. 04:17
728x90

다른 데이터형이 허용되는 데이터의 집합

 

#include <iostream>

using namespace std;

struct MyStruct
{
    string name;
    string job;
    int age;
    float height;
};


int main()
{
    MyStruct A;
    A.name = "홍길동";
    A.job = "개발자";
    A.age = 30;
    A.height = 180.5;

    MyStruct B = {"김철수", "디자이너", 25, 170.5};

    cout << "이름: " << A.name << endl;
    cout << "직업: " << A.job << endl;
    cout << "나이: " << A.age << endl;
    cout << "키: " << A.height << endl;

    cout << "이름: " << B.name << endl;
    cout << "직업: " << B.job << endl;
    cout << "나이: " << B.age << endl;
    cout << "키: " << B.height << endl;
    
    return 0;
}

 

 

typedef 를 사용한 구조체 선언

 

typedef struct {
    int id;
    char name[50];
    float score;
} Student;

Student s1;
s1.id = 1;
strcpy(s1.name, "Kim");
s1.score = 85.5;
728x90