C 无法转换‘;字符(*)[1024]’;至‘;字符*’;作为回报
我想逐行读取一个简单的文件,并将内容保存到数组中。如果编译源代码,则会出现以下错误:C 无法转换‘;字符(*)[1024]’;至‘;字符*’;作为回报,c,readfile,C,Readfile,我想逐行读取一个简单的文件,并将内容保存到数组中。如果编译源代码,则会出现以下错误: test.C: In function ‘char* read_file(int, int, const char*)’: test.C:14:11: error: cannot convert ‘char (*)[1024]’ to ‘char*’ in return return words; ^ 这意味着什么?为什么我不能返回2D数组的指针?这是源代码 #include &
test.C: In function ‘char* read_file(int, int, const char*)’:
test.C:14:11: error: cannot convert ‘char (*)[1024]’ to ‘char*’ in return
return words;
^
这意味着什么?为什么我不能返回2D数组的指针?这是源代码
#include <stdio.h>
#include <string.h>
char * read_file(int buf, int size, const char *fname){
char words[size][buf];
FILE *fptr = NULL;
int idx = 0;
fptr = fopen(fname, "r");
while(fgets(words[idx], buf, fptr)) {
words[idx][strlen(words[idx]) - 1] = '\0';
idx++;
}
return words;
}
int main(void) {
char * words;
words = read_file(1024, 100, "text.txt");
int i;
for(i = 0; i < 100; ++i)
printf("%s\n", words[i]);
}
#包括
#包括
char*read_文件(int buf、int size、const char*fname){
字符字[size][buf];
文件*fptr=NULL;
int-idx=0;
fptr=fopen(fname,“r”);
而(fgets(单词[idx]、buf、fptr)){
单词[idx][strlen(单词[idx])-1]='\0';
idx++;
}
返回单词;
}
内部主(空){
字符*单词;
words=read_文件(1024100,“text.txt”);
int i;
对于(i=0;i<100;++i)
printf(“%s\n”,字[i]);
}
您实际上有两个问题:第一个是关于错误。char
数组的数组与指向char
的指针不同。当数组衰减为指针时,char
数组的指针相当于char
数组的指针,或者在特定情况下,char(*)[buf]
第二个问题更糟糕,那就是您试图返回一个指向局部变量的指针。一旦函数返回,局部变量就会超出范围,基本上不存在。因此,一旦函数返回,指针就不再有效
由于使用可变长度数组,因此有一种解决方案是动态分配数组,作为指向char
的指针,即char**
,并返回该指针。另一种解决方案是将数组作为参数传递
在我看来,最简单的解决方案是在
main
函数中声明数组并将其传递给函数,这样您就不必为指针和动态分配而烦恼,更确切地说是以后释放内存
大概是这样的:
#define STRING_COUNT 100 // Number of strings
#define STRING_SIZE 1024 // Size of each string
// Open and read the file, returns the number of "words" read
size_t read_file(const char *filename, char (*words)[STRING_SIZE])
{
// Implement the function...
// Don't forget to return the number of "words", actually lines, you read
}
int main(void)
{
char words[STRING_COUNT][STRING_SIZE];
size_t number_words = read_file("test.txt", words);
// Use the "words" here...
...
return 0;
}
实际上有两个问题:第一个是关于错误。char
数组的数组与指向char
的指针不同。当数组衰减为指针时,char
数组的指针相当于char
数组的指针,或者在特定情况下,char(*)[buf]
第二个问题更糟糕,那就是您试图返回一个指向局部变量的指针。一旦函数返回,局部变量就会超出范围,基本上不存在。因此,一旦函数返回,指针就不再有效
由于使用可变长度数组,因此有一种解决方案是动态分配数组,作为指向char
的指针,即char**
,并返回该指针。另一种解决方案是将数组作为参数传递
在我看来,最简单的解决方案是在
main
函数中声明数组并将其传递给函数,这样您就不必为指针和动态分配而烦恼,更确切地说是以后释放内存
大概是这样的:
#define STRING_COUNT 100 // Number of strings
#define STRING_SIZE 1024 // Size of each string
// Open and read the file, returns the number of "words" read
size_t read_file(const char *filename, char (*words)[STRING_SIZE])
{
// Implement the function...
// Don't forget to return the number of "words", actually lines, you read
}
int main(void)
{
char words[STRING_COUNT][STRING_SIZE];
size_t number_words = read_file("test.txt", words);
// Use the "words" here...
...
return 0;
}
'字符字[size][buf];'-自动存储,>返回后消失。<无论转换错误如何,您都不想执行此操作。拉桑评论中的链接很重要。“char words[size][buf];”自动存储,>返回后消失。<无论转换错误如何,您都不想执行此操作。Lashane评论中的链接很重要。嗨,谢谢你的回答,你能给我一个简短的例子吗?@Sam添加了简单(不完整)示例,展示了如何将数组作为参数传入。谢谢,我会看一看。嗨,谢谢你的回答,你能给我一个简短的例子吗?@Sam添加了简单(不完整)示例,演示如何将数组作为参数传入。谢谢,我来看看。