struct로 만든 구조체를 함수에 넣어주려면 &를 붙여야하고

함수에서 파라미터 선언을 할때는 *를 붙여야 한다.

 

예제코드로 살펴보자

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

void print(struct tagAddress* pad);

struct tagAddress
{
	char name[30];		// 이름
	int *phone;		// 전화
	char address[100];	// 주소
};

void main(void)
{
	struct tagAddress ad;

	strcpy(ad.name, "홍길동");
	ad.phone = 12345678;
	strcpy(ad.address, "서울시 양천구 목동아파트 13단지");

	print(&ad);
}

void print(struct tagAddress* pad) //함수에서 포인터 구조체로 선언을 해주었다.
{
	printf("이름 : %s \n", pad->name);
	printf("전화 : %d \n", pad->phone);
	printf("주소 : %s \n", pad->address);

	//파라미터를 가져와 저장을 한번 해본다.
	printf("======파라미터 저장 후===== \n");
	
	strcpy(pad->name, "정쓰1");
	pad->phone =1234;
	//memcpy(*((*pad).phone), tempPhone, sizeof(int));
	strcpy(pad->address, "정쓰1의 주소");

	printf("이름2 : %s \n", pad->name);
	printf("전화2 : %d \n", pad->phone);
	printf("주소2 : %s \n", pad->address);

}

 

*는 쓸때는 2가지이다.

1. 포인터 변수를 선언 할때

2. 포인터로 선언한 A에 B를 복사하고 싶을 때 *를 A앞에 씀. 그리고 B는 주소값이 아니라 실제값이어야 함.

(만약 주소 값을 복사하고 싶으면 A는 그대로 B앞엔 &를 붙혀줌)

+ Recent posts