Programming/C , C++
구조체 포인터 매개변수
wo_ody
2020. 10. 12. 00:43
안녕하세요 ! 우디입니다 🙇
오늘은 구조체 포인터 매개변수에 대해서 알아보겠습니다.
- typedef : 자료형의 별칭을 만드는 기능
- 구조체 선언
xxxxxxxxxx
반환값 자료형 함수 이름(struct 구조체 이름 *매개변수)
{
}
example )
xxxxxxxxxx
// strcpy 컴파일 에러 방지
struct movie {
char name[10];
int age;
};
//구조체 정의
void setMovie(struct movie *m)//구조체 포인터 매개변수
{
strcpy(m->name, "버즈");
m->age = 13;
}
int main()
{
struct movie toy_story;
strcpy(toy_story.name, "우디");
toy_story.age = 23;
setMovie(&toy_story);//매개변수 만들어놓은 구조체 toy_story 주소값 담아줌.
printf("이름 : %s \n", toy_story.name);
printf("나이 : %d \n", toy_story.age);
return 0;
}
- typedef 이용
xxxxxxxxxx
// strcpy 컴파일 에러 방지
typedef struct moi_ve {
char name[10];
int age;
}movie,*mmovie;
//구조체 정의
void setMovie(mmovie m)//구조체 포인터 매개변수
{
strcpy(m->name, "버즈");
m->age = 13;
}
int main()
{
movie toy_story;
strcpy(toy_story.name, "우디");
toy_story.age = 23;
setMovie(&toy_story);//매개변수 만들어놓은 구조체 toy_story 주소값 담아줌.
printf("이름 : %s \n", toy_story.name);
printf("나이 : %d \n", toy_story.age);
return 0;
}