구조체 포인터

Jaemyeong Lee·2024년 6월 7일

C/C++

목록 보기
38/56

GPT

C/C++에서 구조체 포인터에 대해 설명하고, 이를 이해하기 위한 다양한 방식을 제공하겠습니다.

1. 구조체 포인터 개요

구조체 포인터는 구조체 변수의 메모리 주소를 저장하는 포인터입니다. 구조체 포인터를 사용하면 구조체 변수의 주소를 통해 구조체 멤버에 접근하고, 동적 할당을 통해 구조체를 효율적으로 관리할 수 있습니다.

2. 구조체 포인터 선언과 사용법

구조체와 구조체 포인터 선언

#include <iostream>
#include <cstring>

struct Person {
    char name[50];
    int age;
    float height;
};

int main() {
    Person person;
    Person* ptr = &person; // 구조체 포인터 선언 및 초기화

    // 구조체 멤버 접근
    strcpy(ptr->name, "John Doe");
    ptr->age = 30;
    ptr->height = 5.9;

    std::cout << "Name: " << ptr->name << std::endl;
    std::cout << "Age: " << ptr->age << std::endl;
    std::cout << "Height: " << ptr->height << std::endl;

    return 0;
}

주요 연산자

  • -> (멤버 접근 연산자): 포인터가 가리키는 구조체의 멤버에 접근합니다.
  • * (간접 연산자): 포인터가 가리키는 구조체를 참조합니다.

3. 컴퓨터 구조 및 운영체제 측면에서의 이해

메모리 할당

구조체 변수와 구조체 포인터는 스택 메모리 또는 힙 메모리에 할당될 수 있습니다. 구조체 포인터를 사용하여 구조체를 동적으로 할당하면 힙 메모리에 저장됩니다.

#include <iostream>
#include <cstring>

struct Person {
    char name[50];
    int age;
    float height;
};

int main() {
    // 동적 메모리 할당
    Person* ptr = new Person;

    // 구조체 멤버 접근
    strcpy(ptr->name, "Jane Doe");
    ptr->age = 25;
    ptr->height = 5.7;

    std::cout << "Name: " << ptr->name << std::endl;
    std::cout << "Age: " << ptr->age << std::endl;
    std::cout << "Height: " << ptr->height << std::endl;

    // 동적 메모리 해제
    delete ptr;

    return 0;
}

메모리 접근

구조체 포인터를 통해 구조체 멤버에 접근할 때, 포인터가 가리키는 주소를 사용하여 해당 멤버의 값을 읽거나 쓸 수 있습니다.

구조체 메모리 배치:
struct Person {
    char name[50];
    int age;
    float height;
};

+-----------------+
|      name[50]   |
+-----------------+
|      age        |
+-----------------+
|      height     |
+-----------------+

포인터 사용:
ptr -> 구조체의 주소
ptr->name -> 구조체의 name 멤버
ptr->age -> 구조체의 age 멤버
ptr->height -> 구조체의 height 멤버

4. 그림과 삽화

구조체 포인터의 메모리 구조와 멤버 접근 방식을 이해하기 쉽게 그림과 표로 표현하였습니다.

구조체 선언:
struct Person {
    char name[50];
    int age;
    float height;
};

메모리 배치:
+-----------------+
|      name[50]   |
+-----------------+
|      age        |
+-----------------+
|      height     |
+-----------------+

포인터 선언:
Person* ptr = &person;

포인터 사용:
ptr -> 구조체의 주소
ptr->name -> 구조체의 name 멤버
ptr->age -> 구조체의 age 멤버
ptr->height -> 구조체의 height 멤버

5. 예제 코드 확장

다양한 상황에서 구조체 포인터를 사용하는 예제를 추가로 제공합니다.

함수와 구조체 포인터

#include <iostream>
#include <cstring>

struct Person {
    char name[50];
    int age;
    float height;
};

// 구조체 포인터를 인수로 받는 함수 선언
void printPerson(Person* p) {
    std::cout << "Name: " << p->name << std::endl;
    std::cout << "Age: " << p->age << std::endl;
    std::cout << "Height: " << p->height << std::endl;
}

int main() {
    Person person;
    Person* ptr = &person;

    // 구조체 멤버 초기화
    strcpy(ptr->name, "John Doe");
    ptr->age = 30;
    ptr->height = 5.9;

    // 함수 호출
    printPerson(ptr);

    return 0;
}

동적 배열과 구조체 포인터

#include <iostream>
#include <cstring>

struct Person {
    char name[50];
    int age;
    float height;
};

int main() {
    int numPersons = 3;
    // 동적 배열 할당
    Person* persons = new Person[numPersons];

    // 배열 요소 초기화
    strcpy(persons[0].name, "John Doe");
    persons[0].age = 30;
    persons[0].height = 5.9;

    strcpy(persons[1].name, "Jane Smith");
    persons[1].age = 25;
    persons[1].height = 5.7;

    strcpy(persons[2].name, "Alice Johnson");
    persons[2].age = 28;
    persons[2].height = 5.8;

    // 배열 요소 출력
    for (int i = 0; i < numPersons; ++i) {
        std::cout << "Person " << i + 1 << std::endl;
        std::cout << "Name: " << persons[i].name << std::endl;
        std::cout << "Age: " << persons[i].age << std::endl;
        std::cout << "Height: " << persons[i].height << std::endl;
        std::cout << std::endl;
    }

    // 동적 메모리 해제
    delete[] persons;

    return 0;
}

요약

구조체 포인터는 구조체 변수의 메모리 주소를 저장하는 포인터로, 이를 통해 구조체 멤버에 접근하고, 동적 할당을 통해 구조체를 효율적으로 관리할 수 있습니다. 구조체 포인터는 -> 연산자를 사용하여 구조체 멤버에 접근하며, 포인터를 사용하여 함수에 구조체를 전달하거나 동적 배열을 관리할 수 있습니다. 운영체제는 구조체 포인터가 사용하는 메모리의 할당과 해제를 관리하여 프로그램이 효율적으로 실행될 수 있도록 돕습니다.

이와 같은 내용을 통해 C/C++의 구조체 포인터에 대해 더 깊이 이해할 수 있을 것입니다. 추가로 궁금한 사항이 있으면 알려주세요!

profile
李家네_공부방

0개의 댓글