當我debug時,並沒有發現錯誤,可是再執行時,才剛輸入一次,便出錯了,可以請教一下大概是哪裡有錯嗎?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char **INPUT;
char *STRING_CONNECT(char *NOW_INPUT){/* connect string "hello, " with the string stroed in NOW_INPUT */
char *HELLO;
HELLO = (char *) malloc(sizeof(char *));
strcpy(HELLO, "hello, ");
strcat(HELLO, NOW_INPUT);
return HELLO;
}
int READ_INPUT(void){ /* STRING_NUM record the number of inputs. TEMP store the input and connected "hello, "
then stroed in NEXT, which point to the final location, INPUT, that store inputs. */
char *TEMP;
char **NEXT;
int STRING_NUM = 0;
TEMP = (char *) malloc(sizeof(char *));
NEXT = INPUT;
while(gets(TEMP)){
++STRING_NUM;
strcpy(*NEXT, STRING_CONNECT(TEMP));
INPUT = (char **) realloc(INPUT, sizeof(*INPUT) * (STRING_NUM+1));
NEXT = INPUT + STRING_NUM;
*NEXT = (char *) malloc(sizeof(**INPUT));
}
return STRING_NUM;
}
void PRINT_RESULT(int n){
int i;
for(i = 0; i < n; ++i)
puts(INPUT[i]);
}
int main(void){
int string_num;
printf("Input strings:\n");
INPUT = (char **) malloc(sizeof(char **));
*INPUT = (char *) malloc(sizeof(char *));
string_num = READ_INPUT();
printf("Hello:\n");
PRINT_RESULT(string_num);
return 0;
}