포인터란?
주소값을 저장하는 변수
ex)
int a = 10;
int *p = &a;
a를 간접참조 하고 있음.
a == *p
&a == p
char *pChar;
printf("%d\n", sizeof(pChar));
printf("%d\n", sizeof(*pChar));
sizeof(pChar) == 8
sizeof(*pChar) == 1
64비트로 컴파일하는 경우,
포인터 변수는 변수타입과 상관없이 8byte이다.
프로그램 실행 중 메모리를 새로 할당 받는 것을 말함!
malloc()함수를 사용 (stdlib.h에 있음)
리턴타입은 void*
ex) 사이즈가 10개인 int타입의 배열을 동적할 당 해보자.
int타입의 10개 사이즈 지정
sizeof(int)*10)
int타입의 10개 사이즈 할당
malloc(sizeof(int)*10)
포인터에 할당
int *my = malloc(sizeof(int)*10)
(내가 할당하고자 하는 타입과 pointer변수타입은 같아야함.)
포인터변수 타입에 맞게 형변환
int my = (int*)malloc(sizeof(int)10)
다음 변수가 들어갈 만큼의 변수를 동적할당 하자~
char param[20] = {"cos pro"}
sizeof(char)*strlen(param)malloc(sizeof(char)*strlen(param)) //strlen은 string.h에 있음char *ch1 = malloc(sizeof(char)*strlen(param))char *ch1 = (char*)malloc(sizeof(char*strlen(param))