구조체: 하나 이상의 변수(포인터 변수와 배열 포함)를 묶어서 새로운 자료형을 정의하는 도구
struct point //point라는 이름의 구조체 정의
{
int xpos; //point 구조체를 구성하는 멤버 xpos
int ypos; //point 구조체를 구성하는 멤버 ypos
};
point라는 이름이 int나 double과 같은 자료형의 이름이 된다. 이를 가리켜 '사용자 정의 자료형'이라 한다.
배열도 구조체의 멤버가 될 수 있다.
위에서 point라는 이름의 구조체를 정의하였으므로 point 자료형을 대상으로 변수를 선언할 수 있다. 이렇게 선언되는 변수를 가리켜 '구조체 변수'라 한다.
struct type_name val_name; //구조체 변수 선언의 기본 형태
struct point pos;
구조체 변수 안에 존재하는 멤버에 접근하는 방식은 다음과 같다.
구조체 변수의 이름. 구조체 멤버의 이름
pos.xpos = 20;
printf("%d \n", pos.ypos);
struct point
{
int xpos;
int ypos;
} pos1, pos2, pos3;
Q. 문자열 형태의 ‘종업원 이름’과 문자열 형태의 ‘주민등록번호’ 그리고 정수 형태의 ‘급여정보’를 저장할 수 있는 employee라는 이름의 구조체를 정의해보자. 그리고 나서 employee 구조체 변수를 하나 선언한 다음, 프로그램 사용자가 입력하는 정보로 이 변수를 채우자. 그리고 마지막으로 구조체 변수에 채워진 데이터를 출력해보자.
A.
#include <stdio.h>
struct employee
{
char name[20];
char num[20];
int money;
};
int main(void)
{
struct employee e1;
printf("이름: ");
scanf("%s", e1.name);
printf("주민번호: ");
scanf("%s", e1.num);
printf("급여: ");
scanf("%d", &e1.money);
printf("\n");
printf("이름: %s \n", e1.name);
printf("주민번호: %s \n", e1.num);
printf("급여: %d \n", e1.money);
return 0;
}
#include <stdio.h>
struct point
{
int xpos;
int ypos;
};
int main(void)
{
struct point pos={10, 20};
printf("%d %d \n", pos.xpos, pos.ypos);
return 0;
}
#include <stdio.h>
struct point
{
int xpos;
int ypos;
};
int main(void)
{
struct point arr[3];
int i;
for(i=0; i<3; i++)
{
printf(H점의 좌표 입력: ");
scanf("%d %d", &arr[i].xpos, &arr[i].ypos);
}
for(i=0; i<3; i++)
printf("[%d, %d] ", arr[i].xpos, arr[i].ypos);
return 0;
}
struct person arr[3] = {
{"이승기", "010-1212-0001", 21}, // 첫 번째 요소의 초기화
{"정지영", "010-1313-0002", 22}, // 두 번째 요소의 초기화
{"한지수", "010-1717-0003", 19} // 세 번째 요소의 초기화
};
Q. 앞서 문제 22-1 에서 정의한 employee 구조체를 기반으로 길이가 3인 배열을 선언하자. 그리고 세 명의 정보를 프로그램 사용자로부터 입력 받아서 배열에 저장한 다음, 배열에 저장된 데이터를 순서대로 출력하는 예제를 작성해보자.
A.
#include <stdio.h>
struct employee
{
char name[20];
char num[20];
int money;
};
int main(void)
{
struct employee emp[3];
int i;
for (i = 0; i < 3; i++)
{
printf("%d번 \n", i + 1);
printf("이름: ");
scanf("%s", emp[i].name);
printf("주민번호: ");
scanf("%s", emp[i].num);
printf("급여: ");
scanf("%d", &emp[i].money);
printf("\n");
}
printf("\n");
for (i = 0; i < 3; i++)
{
printf("%d번 \n", i + 1);
printf("이름: %s \n", emp[i].name);
printf("주민번호: %s \n", emp[i].num);
printf("급여: %d \n", emp[i].money);
}
return 0;
}
struct point pos = {11, 12}; // xpos^ ypos를 각각 11, 12로 초기화
struct point * pptr = &pos; // 포인터 변수 pptr이 구조체 변수 pos를 가리킴
(*pptr).xpos=10; // pptr이 가리키는 구조체 변수의 멤버 xpos에 10 저장
(*pptr).ypos=20; // pptr이 가리키는 구조체 변수의 멤버 ypos에 20 저장
//또는
pptr->xpos=10; // pptr이 가리키는 구조체 변수의 멤버 xpos에 10 저장
pptr->ypos=20; // pptr이 가리키는 구조체 변수의 멤버 ypos에 20 저장
*연산과 .연산을 하나의 ->연산으로 대신할 수 있다.
포인터 변수도 구조체의 멤버가 될 수 있다.
struct point
{
int xpos;
int ypos;
};
struct circle
{
double radius;
struct point * center;
};
또한, TYPE형 구조체 변수의 멤버로 TYPE형 포인터 변수를 둘 수 있다.
struct point
{
int xpos;
int ypos;
struct point * ptr; // 구조체 point의 포인터 변수 선언
}
p.467 예제
3~8행을 보면 point형 구조체는 멤버가 3개인데
12~13행에서 초기화를 2개밖에 안 한다(xpos, ypos인듯).
이래도 되는 건가?
-> 16~18행에 ptr을 초기화했으므로 나눠서 초기화할 수 있는 것으로 보인다.
구조체 변수의 주소 값은 구조체 변수의 첫 번째 멤버의 주소 값과 동일하다.
struct point
{
int xpos;
int ypos;
}
int main(void)
{
struct point pos = {10, 20};
}
이때 pos의 주소 값과 pos.xpos의 주소 값이 같다.