【发布时间】:2018-03-03 07:39:30
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *strs[6], array[100];
int i;
FILE *file = fopen("random.txt", "r");
for (i=0 ; i<6; i++)
{
fscanf(file ,"%s", array);
strs[i]= array;
printf("%s ", strs[i]);
}
printf("%s", strs[3]);
return 0;
}
random.txt里面:
一二三四五六
输出是: 一二三四五六六
我的问题是为什么最后一个“六”,我无法访问第三个元素“四”。为什么不将它们记录在 char 指针数组中?
【问题讨论】:
-
strs[i]= array;->strs[i]= strdup(array);如果没有strdup使用malloc/memcpy得到相同的。这里所有的指针都指向数组,并且该数组最后包含six。
标签: c