新手:将指针传递给函数

发布于 2025-01-19 15:55:02 字数 1540 浏览 2 评论 0原文

我试图从大约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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千紇 2025-01-26 15:55:02

您有两个选择

首先,让“getfile”返回文件句柄(这是c中最惯用的方式)

FILE *getfile() {  
  FILE *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);
  }
  return fp;
}

并在main中

FILE *fp =  getfile(fp);

,或者让getfile更新基于fp值,

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);
  }
}

在main中使用c风格的“按引用传递”

File *fp = NULL;
getfile(&fp);

You have two choices

First, have 'getfile' return the file handle (this is the most idiomatic way in c)

FILE *getfile() {  
  FILE *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);
  }
  return fp;
}

and in main

FILE *fp =  getfile(fp);

or have getfile update the fp value based , using c-style 'pass by reference'

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);
  }
}

in main

File *fp = NULL;
getfile(&fp);
放血 2025-01-26 15:55:02

fp 不应该是 getfile() 的参数,它应该是返回值。

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

struct arguments {
  char *word_file;  /* Default name for input file */
} arguments;

FILE *getfile() {  
  FILE *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);
  }
  return fp;
}

int main (int argc, char *argv[]) {

  arguments.word_file = "dummy";
  
  FILE *file_ptr;
  file_ptr = getfile();
  
  char word[60];
  fgets (word, sizeof(word), file_ptr);
  printf("Word is %s\n", word);
}

如果出于某种原因您确实需要将其作为参数传递,请参阅更改包含的地址通过指针使用函数

fp shouldn't be an argument to getfile(), it should be the return value.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

struct arguments {
  char *word_file;  /* Default name for input file */
} arguments;

FILE *getfile() {  
  FILE *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);
  }
  return fp;
}

int main (int argc, char *argv[]) {

  arguments.word_file = "dummy";
  
  FILE *file_ptr;
  file_ptr = getfile();
  
  char word[60];
  fgets (word, sizeof(word), file_ptr);
  printf("Word is %s\n", word);
}

If there's some reason you really need to pass it as a parameter, see Changing address contained by pointer using function

当爱已成负担 2025-01-26 15:55:02

问题是指针fp按值传递给函数getfile

getfile(fp);

这就是函数处理传递指针fp的值的副本。因此,更改函数中的副本不会反映原始指针的值。

您需要通过参考将指针传递。

在C传递的C中,意味着间接通过指向其指针的对象。删除指针您可以直接访问原始对象。

因此,声明并定义函数

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);
  }
}

,并将功能称为

getfile(&fp);

The problem is that the pointer fp is passed to the function getfile by value.

getfile(fp);

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

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);
  }
}

and call the function like

getfile(&fp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文