- 论坛徽章:
- 0
|
如下代码中,向一个函数中传入一个FILE指针。为什么我传一个FILE的地址进去,和返回一个FILE型指针会出现不同的结果?
用返回的FILE指针,结果正常;用传址进去的,段错误。这是为什么,麻烦大家帮我解释一下谢谢!- int main(void)
- {
- char *fname = "/usr/local/nilex/crc/conf.d/crc.conf";
- FILE *fp, file_p;
- char value[WORD_MAX];
- int ret;
- if((fp = init_conf(&file_p, fname)) == NULL)
- return -1;
- char tmp[512];
- fgets(tmp, sizeof(tmp), fp);
- printf("fp tmp: %s\n", tmp);
- fgets(tmp, sizeof(tmp), &file_p);
- printf("tmp: %s", tmp);
-
- ... ...
- return ret;
- }
- FILE *init_conf(FILE *fp, char *path)
- {
- if((fp = fopen(path, "rt")) == NULL)
- {
- printf("cannot open file %s\n", *path);
- return NULL;
- }
-
- return fp;
- }
复制代码 |
|