[英]fopen incompatible type char (*)[9] instead of char(*) in C
My program in C reads data in a file in order to initialise variables but it won't open the file. 我用C编写的程序读取文件中的数据以初始化变量,但不会打开文件。 It fails when it reaches fopen.
到达fopen时它将失败。 Xcode outputs the following warning for both fopen and printf.
Xcode对fopen和printf输出以下警告。 I understand the error but I don't know how to correct it, I tried many tricks but it won't do, can anyone help me out ?
我理解该错误,但是我不知道如何纠正该错误,我尝试了很多技巧,但没有成功,有人可以帮助我吗? I just want my program to open Try1.txt .
我只希望我的程序打开Try1.txt 。
Incompatible pointer types passing 'char (*)[9]' to parameter of type const char*' 不兼容的指针类型将'char(*)[9]'传递给const char *类型的参数
So this is the code inside my main function : 这是我主要功能内的代码:
FILE *infile = NULL;
const char infilename [] = "Try1.txt";
infile = fopen(&infilename, "r");
if (infile == NULL)
{
printf("Failed to open file: %s\n", &infilename);
return 1;
}
Note that the program stops before reaching the if loop because it never prints. 注意,该程序在到达if循环之前停止,因为它从不打印。 I tried to initialise the size and to add '\\0' at the end of my string too.
我尝试初始化大小,并在字符串的末尾添加'\\ 0'。
It's because of &infilename
, which gives you a pointer to the array of characters. 这是因为
&infilename
,它为您提供了指向字符数组的指针。 Drop the address-of operator and it will work. 删除操作员的地址,它将起作用。
&infilename
is a pointer to the array, which means it is a pointer to an object of type char (*)[9]
. &infilename
是指向数组的指针,这意味着它是指向char (*)[9]
类型的对象的指针。
But, infilename
will be a pointer of type char (*)
, which points to the first element of the array. 但是,
infilename
将是char (*)
类型的指针,该指针指向数组的第一个元素。
infile = fopen(infilename, "r");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.