如何检索文件中由空格分隔的单词,并将这些单词分别存储在C中的指针数组中
//程序从文件中读取单词并将它们存储在指针数组中,然后再访问它们 int main {如何检索文件中由空格分隔的单词,并将这些单词分别存储在C中的指针数组中,c,C,//程序从文件中读取单词并将它们存储在指针数组中,然后再访问它们 int main { 返回0;首先不要强制转换malloc的结果 w=malloc(sizeof (char));// here you are allocating the one byte. 将其更改为必须存储的take字节 w=malloc(1024); 在使用指针数组时,每个指针都必须为指向不同指针的每个指针分配内存 while(fscanf(f, "%s", w) != EOF && i <
返回0;首先不要强制转换malloc的结果
w=malloc(sizeof (char));// here you are allocating the one byte.
将其更改为必须存储的take字节
w=malloc(1024);
在使用指针数组时,每个指针都必须为指向不同指针的每个指针分配内存
while(fscanf(f, "%s", w) != EOF && i < MAX)
{
printf("%s ",w);
word[i]=w;
w=malloc(1024); // It is for next index will point to another memory.
i++;
}
确实要在此处仅分配1个字节:w=char*mallocsizeof char;
while(fscanf(f, "%s", w) != EOF && i < MAX)
{
printf("%s ",w);
word[i]=w;
w=malloc(1024); // It is for next index will point to another memory.
i++;
}