【发布时间】:2015-05-22 18:32:04
【问题描述】:
getLine 返回存储标准输入字符的数组。我尝试打印出数组中的字符串,只得到一些其他字符,而不是输入。函数有什么问题?
char *file_name;
file_name = getLine("enter the file name");
printf("%s\n", file_name);
char *getLine(char *question){
printf("%s\n", question);
char c, answer[100];
int i = 0;
c = getchar();
while (c != '\n'){
answer[i++] = c;
c = getchar();
}
answer[i] = '\0';
return answer;
}
【问题讨论】:
-
您正在返回指向堆栈上 summat 的指针。这在函数结束时被销毁