在以下程序中,file_ptr 为 NULL,但它正在正确初始化。为什么?
#include <stdio.h>
#include <stdlib.h>
void Fopen(const char * restrict filePath, const char * restrict mode, FILE * restrict filePtr);
int main(int argc, char ** argv)
{
FILE * file_ptr = NULL;
Fopen("dummy.dat", "r", file_ptr);
printf("File pointer is %p\n", file_ptr);
exit(0);
}
void Fopen(const char * restrict filePath, const char * restrict mode, FILE * restrict filePtr)
{
if ( (filePtr = fopen(filePath, mode)) != NULL)
printf("file pointer is %p\n", filePtr);
}
在命令行上:
[jim@cola c++]$ ./readFile
file pointer is 0x740010
File pointer is (nil)
唯一的解释是 FILE * 的副本正在传递给 Fopen。我如何通过 ref 传递指针?