文件I/O代码中的指针声明

-1 c pointers

我无法理解如何声明指针的代码.

#include <stdio.h>

void openfile(char *, FILE **);
int main()
{
     FILE *fp;
     openfile("Myfile.txt",fp);

     if (fp == NULL)
        printf("Unable to open file..\n");

     fclose(fp);
     return 0;

}

void openfile(char *fn, FILE **f)
{
    *f = fopen(fn,"r");
}
Run Code Online (Sandbox Code Playgroud)

当我跑到上面的程序时,我得到2个警告......

file.c:9:3: warning: passing argument 2 of ‘openfile’ from incompatible pointer type [enabled by default]
     openfile("Myfile.txt",fp);
                           ^
file.c:3:6: note: expected ‘struct FILE **’ but argument is of type ‘struct FILE *’
void openfile(char *, FILE **);    
                   ^
Run Code Online (Sandbox Code Playgroud)

这些警告意味着什么?请问您能解释一下,如何在上面的代码中使用指针?

Mik*_*CAT 5

fpFILE*宣布,而不是FILE**.

FILE*是"指针FILE".

FILE**是'指针'指向"'的指针FILE.

openfile(),它想要更新FILE*数据,因此FILE*请求指针.

总之,你应该openfile("Myfile.txt",&fp);使用openfile().

这意味着你应该使用&操作符来获得的地址fp更新fpopenfile().