Programming/C , C++
[ C ] struct를 이용한 구조체
wo_ody
2020. 10. 12. 00:43
안녕하세요 ! 우디입니다.🙃
오늘은 구조체 정의와 선언에 대해 알려드릴께요 ~
- 구조체는 struct 키워드로 정의합니다.
xxxxxxxxxx
struct tag(구조체 이름){
자료형 멤버이름
};
- 구조체 선언
xxxxxxxxxx
struct tag(구조체 이름) 변수이름;
example )
xxxxxxxxxx
// strcpy 컴파일 에러 방지
struct movie{
char name[10];
int age;
};//구조체 정의
int main()
{
struct movie toy_story;//구조체 변수 선언
//점으로 구조체 멤버변수에 접근 !
strcpy(toy_story.name,"우디");
toy_story.age=23;
printf("이름 : %s \n",toy_story.name);
printf("나이 : %d \n",toy_story.age);
return 0;
}
이번에는 구조체 정의와 동시에 변수 선언하는 방법에 대해 알아보겠습니다.
앞에서 구조체 정의한 것과 차이점은 닫는 중괄호( } )와 세미콜론 ( ; ) 사이에 변수이름을 넣는 다는 것 입니다 !
- 구조체 정의와 동시에 변수 선언
xxxxxxxxxx
struct tag(구조체 이름){
자료형 멤버이름
} 변수이름 ;
example )
xxxxxxxxxx
// strcpy 컴파일 에러 방지
struct movie{
char name[10];
int age;
}toy_story;//구조체 정의와 동시에 변수 선언
int main()
{
//점으로 구조체 멤버변수에 접근 !
strcpy(toy_story.name,"우디");
toy_story.age=23;
printf("이름 : %s \n",toy_story.name);
printf("나이 : %d \n",toy_story.age);
return 0;
}