【发布时间】:2020-11-13 21:49:41
【问题描述】:
我想请用户给我五个问题,然后我想打印出这五个问题。我正在使用 for 循环。它适用于第一个问题,然后停止工作。提前谢谢你。
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char *question [5];
question[5] = (char*) malloc(5 * sizeof(char));
for (int i = 0; i < 5; ++i)
{
printf("enter question number %i\n", i+1);
scanf("%[^\n]%*c", question[i]);
printf("%s\n", question[i]);
}
free (question[5]);
return 0;
}
【问题讨论】:
-
question[5] = (char*) malloc(5 * sizeof(char));- 这是为 5 个chars分配空间并分配给一个超出questions边界的指针。绝对不是你想做的。您需要为question[0]...question[4]中的每一个分配空间
标签: arrays c for-loop pointers