我只是想 malloc 一个字符串数组,并将文件中的输入复制到这个数组中。这种行组合会导致段错误,我不知道为什么。
int count = 0;
char **output = (char**)malloc(numLines*257);
fgets(output[count], 257, input);
最佳答案
您已经为一个指针数组分配了空间,但是您还没有初始化任何这些指针。
int count = 0;
char **output = malloc(numLines*sizeof(char *));
int i;
for (i = 0; i < numLines; i++) {
output[i] = malloc(257);
}
fgets(output[count], 257, input);
关于c - 为什么这会导致段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19301733/