save_history(history_first,tok);
history_cur = history_first;
if(history_first==NULL)
printf("fuck2\n");
printf("%s\n",history_first->command);
}
else
save_history(history_cur,tok);
}
if(print==1)
print_history(history_first);
}
return 0;
}
void save_history(struct HISTORY *cur,char *command)
{
struct HISTORY *temp;
printf("2\n");
if(cur==NULL) // 첫 번째 입력일 경우
{
printf("4\n");
cur = (struct HISTORY*)malloc(sizeof(struct HISTORY));
strcpy(cur->command,command);
if(cur==NULL)
printf("fuck\n");
printf("%s\n",cur->command);
소스의 일부분입니다.
위쪽의 굵은글씨에서 save_history를 호출해서 malloc을 해주고 command라는 char를 넣어주는데,
함수 안에서는 NULL 검사도 통과하고 출력도 제대로 되지만
함수가 끝나고
if(history_first==NULL)
printf("fuck2\n");
printf("%s\n",history_first->command);
이 부분에서는 NULL검사 통과 못하고 printf에서 세그멘테이션 에러가 뜨는데
이유가 뭔가요; 함수 리턴형이 void라서 그런건 아닐거고 이해가 안됨 ㅠㅠ
소스가 짤려서 뭔지 모르겠다
struct a *b 를 1이라는 함수 안에다가 잡고, b를 파라미터로 넘겨서 2라는 함수에서 malloc을 해서 자료를 집어넣고 나온 상태인데 2 함수가 종료되면 그 malloc 했던 것이 사라지는지;
자 save_history 함수 안에서 인자인 cur를 변경시키면 그게 함수 밖의 인자에게 적용이 될까 안될까
call by value의 문제지
흠; 교수님이 전역변수 쓰지 말라고 해서 ㅠㅠ
인자가 *라고 해서 무조건 call by reference가 아닌거군요; 좋은거 알아갑니다 ㄳㄳ
전역변수를 쓸필요는 없지. struct HISTORY*를 변경시키고 싶으면 인자로는 struct HISTORY**를 넘기면 된다
아.. ㅠㅠ 감사합니다
현아이유