我无法理解如何声明指针的代码.
#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)
这些警告意味着什么?请问您能解释一下,如何在上面的代码中使用指针?
fp是FILE*宣布,而不是FILE**.
FILE*是"指针FILE".
FILE**是'指针'指向"'的指针FILE.
在openfile(),它想要更新FILE*数据,因此FILE*请求指针.
总之,你应该openfile("Myfile.txt",&fp);使用openfile().
这意味着你应该使用&操作符来获得的地址fp更新fp在openfile().