简体   繁体   English

fopen不兼容的类型char(*)[9]而不是C中的char(*)

[英]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. 删除操作员的地址,它将起作用。

Problem 问题

&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 (*)类型的指针,该指针指向数组的第一个元素。

Fix 固定

infile = fopen(infilename, "r");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C:从char类型分配给char []类型时,类型不兼容* - C: Incompatible type when assigning to type char[] from type char * 用C将char *传递给fopen - Passing char * into fopen with C “ char”类型的参数与char *类型的参数不兼容 - Argument of type “char” is incompatible with parameter of type char* &char和char指针是不兼容的指针类型? - &char and char pointer is incompatible pointer type? C-从'char *'类型分配给'char * [((sizetype)(NColDataType)]]类型时不兼容的类型 - C - incompatible types when assigning to type 'char *[(sizetype)(NColDataType)]' from type 'char *' fopen在C ++中使用char *获取错误 - fopen get error with char * in c++ * Beginner * C:不兼容的整数到指针转换将'char'传递给'const char *'类型的参数 - *Beginner* C: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *' (从 function 返回 char (*)(255),返回类型不兼容 'char **')在 C 中出错 - (Returning char (*)(255) from a function with incompatible return type 'char **') error in C “volatile char *”类型的参数与“const char *”类型的参数不兼容 - Argument of type “volatile char *” is incompatible with parameter of type “const char *” 从“ char *”类型分配给“ char [50]”类型时不兼容的类型 - incompatible types when assigning to type 'char[50]' from type 'char *'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM