Heap 1번 본문
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
struct internet {
int priority;
char *name;
};
void winner()
{
printf("and we have a winner @ %d\n", time(NULL));
}
int main(int argc, char **argv)
{
struct internet *i1, *i2, *i3;
i1 = malloc(sizeof(struct internet));
i1->priority = 1;
i1->name = malloc(8);
i2 = malloc(sizeof(struct internet));
i2->priority = 2;
i2->name = malloc(8);
strcpy(i1->name, argv[1]);
strcpy(i2->name, argv[2]);
printf("and that's a wrap folks!\n");
}
argv[1] argv[2] 를 보니
인자 값 두 개 넣어주면 된다.
예시로 'a' 4개와 'b' 4개를 넣고 디버깅을 돌렸다.
0x804a198 = i1 영역
0x804a1a8 = i1->name 영역
0x804a1b8 = i2 영역
0x804a1c8 = i2->name 영역
i1->name에서 입력을 받아 i2의 name으로 가는 주소 값을
printf의 got 주소 값으로 바꾼다.
그 후 두번째 인자 값으로 winner 주소를 넣는다.
Comments