#define STRING_SIZE 1024
typedef struct strlink{
char str[STRING_SIZE];
struct strlink * nextString;
struct strlink * prevString;
}strLink;
strLink * head = NULL;
strLink * current = NULL;
strLink * prevPtr = NULL;
strLink * tail = NULL;
char buf[STRING_SIZE];
int strSize=0;
int strReference = 0;
void readCommandLines()
{
head = (strLink *)malloc(STRING_SIZE+8);
current = head;
while(1){
if(fgets(current->str, STRING_SIZE , stdin)==NULL)
break;
current->str[strlen(current->str)-1]='\0';
current->nextString = (strLink *)malloc(STRING_SIZE+8);
prevPtr = current;
current = current->nextString;
current->prevString = prevPtr;
}
tail = current;
}
void printCommands(){
for(current = tail; current !=NULL ; current = current->prevString)
{
printf("Command line : %s\n",current->str);
}
}
입력값
aaa
bbb
ccc
희망 결과 :
Command line : ccc
Command line : bbb
Command line : aaa
실제결과
Command line :
Command line : ccc
세그멘테이션 오류
제가 링크 리스트랑 malloc 제대로 구현한거 맞음?
답좀 부탁 ㅠㅠ
아 참고로 printcommand함수에 n이아니라 \n임
역슬래시엔이 왜 안써지냐
\\\
어쨋든 답좀부탁 ㅠ
current->str[strlen(current->str)-1]=''; -> 역슬레시 제로 역슬레시가 안써지네;
current->str[strlen(current->str)-1]=''; 이건 삭제하고
+8 이것도 삭제하셈 왜하는거임 그러면 널값부터 역순으로 출력될거임
근데 널값은 안넣는게 좋은데 그리고 할당은 왜이렇게 과하게 넣은거임?
또 잔소리하면 메모리는 다시 안날림? 메모리 날리는 습관 기르기를 ㅋㅋㅋ 저도 초보지만 이렇게 지적하는거 처음 ㅋㅋㅋㅋ
마지막으로 널값빼고 출력하는거 원하면 tail=prevPtr; 이걸로 바꾸셈
너가 말한거 다틀렸지만 고맙다
뭔개소리임 내가되는것도확인했는데