2-1 구조체 정의 및 사용 (학생 정보)

문제설명

학생의 정보를 저장하는 구조체를 정의하고 기본 사용을 연습하는 문제이다.

 

목표

  • 구조체 정의
  • 구조체 변수 선언 및 초기화
  • 멤버 접근

요구사항

  • struct student를 정의하시오
    • name[20] (문자열)
    • score (정수)
  • 변수 s1을 선언하고
    • 이름: "Kim"
    • 점수: 90
  • 값을 출력하시오
 

출력예시

Kim 90
 

 

예상코드

더보기
더보기
#include <stdio.h>

struct student {
    char name[20];
    int score;
};

int main(void) {
    struct student s1 = {"Kim", 90};
    printf("%s %d\n", s1.name, s1.score);
    return 0;
}
 

2-2 구조체 배열 (도서 목록)

문제설명

여러 개의 도서 정보를 구조체 배열로 관리하는 문제이다.

 

목표

  • 구조체 배열 이해
  • 배열 요소 접근

요구사항

  • struct book 정의
    • title[20] (문자열)
    • price (정수)
  • 배열 2개 생성
    • "C언어", 20000
    • "자료구조", 25000
  • 반복문으로 출력
 

출력예시

C언어 20000
자료구조 25000

 

 

 

예상코드

더보기
더보기
#include <stdio.h>

struct book {
    char title[20];
    int price;
};

int main(void) {
    struct book arr[2] = {
        {"C언어", 20000},
        {"자료구조", 25000}
    };

    for(int i=0; i<2; i++) {
        printf("%s %d\n", arr[i].title, arr[i].price);
    }

    return 0;
}

 

 

2-3 구조체 포인터 (상품)

문제설명

구조체 포인터를 사용하여 값을 출력하는 문제이다.

 

목표

  • 구조체 포인터 이해
  • (*p). vs ->

 

요구사항

  • struct product 정의
    • name, price
  • 변수 p1 = {"Laptop", 1000} 생성
  • 포인터 p가 p1을 가리키도록 설정
  • 아래 두 방식으로 출력
    • (*p).name
    • p->name
 

 

 출력예시

Laptop 1000
Laptop 1000
 
 
예상코드

 

더보기
더보기
#include <stdio.h>

struct product {
    char name[20];
    int price;
};

int main(void) {
    struct product p1 = {"Laptop", 1000};
    struct product *p = &p1;

    printf("%s %d\n", (*p).name, (*p).price);
    printf("%s %d\n", p->name, p->price);

    return 0;
}

 

2-4: 구조체 내부 포인터 (주문)

문제설명

구조체 안에 포인터를 포함하는 구조를 연습하는 문제이다.

 

목표

  • 구조체 내부 포인터 이해
  • 참조 관계 이해

요구사항

struct order {
    int quantity;
    struct product *item;
};
 
  • product 변수 생성 ("Mouse", 50)
  • order 생성 (수량 3)
  • 상품 이름과 가격 출력
 

출력예시

Mouse 50
 

 

예상코드

더보기
더보기
#include <stdio.h>

struct product {
    char name[20];
    int price;
};

struct order {
    int quantity;
    struct product *item;
};

int main(void) {
    struct product p = {"Mouse", 50};
    struct order o = {3, &p};

    printf("%s %d\n", o.item->name, o.item->price);

    return 0;
}

 

 

2-5 구조체 주소 확인

문제설명

구조체의 메모리 배치 특성을 확인하는 문제이다.

 

목표

  • 구조체 주소 이해

요구사항

struct student s = {"Lee", 80};

printf("%p\n", &s);
printf("%p\n", &s.name);
 

질문) 코드를 완성하여 실행 후 결과를 비교하시오

 

예상 답안

더보기
더보기

두 주소는 동일하다

이유: 구조체 시작 주소 = 첫 번째 멤버 주소

 

코드예시

#include <stdio.h>

struct student {
    char name[20];
    int score;
};

int main(void)
{
    struct student s = {"Lee", 80};

    printf("%p\n", &s);
    printf("%p\n", &s.name);

    return 0;
}

 

 

 

2-6 typedef (회원)

문제설명

typedef를 사용하여 구조체를 간단히 사용하는 문제이다.

 

목표

  • typedef 이해

요구사항

  • 회원 구조체 정의 (name, age)
  • typedef로 Member로 선언
  • 변수 생성 후 출력
 

출력예시

Park 25
 

 

예상 코드

더보기
더보기
#include <stdio.h>

typedef struct {
    char name[20];
    int age;
} Member;

int main(void) {
    Member m = {"Park", 25};
    printf("%s %d\n", m.name, m.age);
    return 0;
}
 
 

+ Recent posts