Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检索文件中由空格分隔的单词,并将这些单词分别存储在C中的指针数组中_C - Fatal编程技术网

如何检索文件中由空格分隔的单词,并将这些单词分别存储在C中的指针数组中

如何检索文件中由空格分隔的单词,并将这些单词分别存储在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 <

//程序从文件中读取单词并将它们存储在指针数组中,然后再访问它们

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 < 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++;
}