当我调用这个函数时
4 char *file_reader(FILE *fp, char *file_name, const char *mode) {
5 char *buffer;
6 fp = fopen(file_name, mode);
7
8 if (fp != NULL) {
9 fseek(fp, 0L, SEEK_END);
10 long s = ftell(fp);
11 rewind(fp);
12 buffer = malloc(s);
13 if (buffer != NULL) {
14 fread(buffer, s, 1, fp);
15 fclose(fp);
16 fp = NULL;
17 }
18 if (fp != NULL)
19 fclose(fp);
20 } else {
21 printf("No such file.\n");
22 return "ERR";
23 }
24 return buffer;
25 }
通过
char *file_content = file_reader(fp, input_file, "r");
gcc一直抱怨
return from incompatible pointer type [enabled by default]
,但我没有看到任何不兼容的指针类型。任何人都知道,谢谢。 最佳答案
作品。用-Wall -Werror -Wextra -pedantic -std=c11
编译它。
#include "stdio.h"
#include "stdlib.h"
FILE* fp;
char* input_file = "asdf.txt";
char *file_reader(FILE *fp, char *file_name, const char *mode) {
char *buffer;
fp = fopen(file_name, mode);
if (fp != NULL) {
fseek(fp, 0L, SEEK_END);
long s = ftell(fp);
rewind(fp);
buffer = malloc(s);
if (buffer != NULL) {
fread(buffer, s, 1, fp);
fclose(fp);
fp = NULL;
}
if (fp != NULL)
fclose(fp);
} else {
printf("No such file.\n");
return "ERR";
}
return buffer;
}
int main() {
char *file_content = file_reader(fp, input_file, "r");
printf("%s\n", file_content);
return 0;
}
关于c - 从不兼容的指针类型返回[默认启用] C语言中的警告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14412419/