新手:将指针传递给函数
我试图从大约5年前从涉足中重新学习。具体来说,我正在尝试学习如何从MAIN提取许多操作并使它们进入函数,以便将它们移至下一个库文件。
这似乎在起作用:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
struct arguments {
char *word_file; /* Default name for input file */
} arguments;
int main (int argc, char *argv[]) {
arguments.word_file = "dummy";
FILE *fp;
fp = fopen(arguments.word_file, "r");
if (fp == NULL) { /* If fopen failed... */
fprintf(stderr, "Error: Unable to open file %s: %s\n",
arguments.word_file, strerror (errno));
exit (8);
}
char word[60];
fgets (word, sizeof(word), fp);
printf("Word is %s\n", word);
}
顺便说一句,“虚拟”是:
$ cat dummy
dog
cat
$
无论我如何尝试,它都会给我编译错误,或者在运行它时会出现seg错误:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
struct arguments {
char *word_file; /* Default name for input file */
} arguments;
void getfile(FILE *fp) {
fp = fopen(arguments.word_file, "r");
if (fp == NULL) { /* If fopen failed... */
fprintf(stderr, "Error: Unable to open file %s: %s\n",
arguments.word_file, strerror (errno));
exit (8);
}
}
int main (int argc, char *argv[]) {
arguments.word_file = "dummy";
FILE *fp;
getfile(fp);
char word[60];
fgets (word, sizeof(word), fp);
printf("Word is %s\n", word);
}
我尝试从 *fp更改为fp fp fp&amp; FP没有成功。我敢肯定,关于文件指针我不了解一些东西,但无法弄清楚。
感谢您的任何帮助和建议。
-kevin
I'm trying to relearn C from dabbling with it about 5 year ago. Specifically, I'm trying to learn how to extract a number of operations from main and make them into a function, with the aim of moving them to a library file next.
This seems to be working:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
struct arguments {
char *word_file; /* Default name for input file */
} arguments;
int main (int argc, char *argv[]) {
arguments.word_file = "dummy";
FILE *fp;
fp = fopen(arguments.word_file, "r");
if (fp == NULL) { /* If fopen failed... */
fprintf(stderr, "Error: Unable to open file %s: %s\n",
arguments.word_file, strerror (errno));
exit (8);
}
char word[60];
fgets (word, sizeof(word), fp);
printf("Word is %s\n", word);
}
By the way, 'dummy' is:
$ cat dummy
dog
cat
$
No matter how I try this, it either gives me compile errors, or seg faults when I run it:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
struct arguments {
char *word_file; /* Default name for input file */
} arguments;
void getfile(FILE *fp) {
fp = fopen(arguments.word_file, "r");
if (fp == NULL) { /* If fopen failed... */
fprintf(stderr, "Error: Unable to open file %s: %s\n",
arguments.word_file, strerror (errno));
exit (8);
}
}
int main (int argc, char *argv[]) {
arguments.word_file = "dummy";
FILE *fp;
getfile(fp);
char word[60];
fgets (word, sizeof(word), fp);
printf("Word is %s\n", word);
}
I've tried changing from *fp to fp to &fp without success. I'm sure that there's something that I don't understand about file pointers, but can't figure it out.
Thanks for any help and suggestions.
-Kevin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有两个选择
首先,让“getfile”返回文件句柄(这是c中最惯用的方式)
并在main中
,或者让getfile更新基于fp值,
在main中使用c风格的“按引用传递”
You have two choices
First, have 'getfile' return the file handle (this is the most idiomatic way in c)
and in main
or have getfile update the fp value based , using c-style 'pass by reference'
in main
fp
不应该是getfile()
的参数,它应该是返回值。如果出于某种原因您确实需要将其作为参数传递,请参阅更改包含的地址通过指针使用函数
fp
shouldn't be an argument togetfile()
, it should be the return value.If there's some reason you really need to pass it as a parameter, see Changing address contained by pointer using function
问题是指针
fp
按值传递给函数getfile
。这就是函数处理传递指针
fp
的值的副本。因此,更改函数中的副本不会反映原始指针的值。您需要通过参考将指针传递。
在C传递的C中,意味着间接通过指向其指针的对象。删除指针您可以直接访问原始对象。
因此,声明并定义函数
,并将功能称为
The problem is that the pointer
fp
is passed to the functiongetfile
by value.That is the function deals with a copy of the value of the passed pointer
fp
. So changing the copy in the function does not reflect on the value of the original pointer.You need to pass the pointer by reference.
In C passing by reference means passing an object indirectly through a pointer to it. Dereferencing the pointer you can get a direct access to the original object.
So declare and define the function like
and call the function like