C语言 将地址传递给 C 中的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5609134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:21:08  来源:igfitidea点击:

Passing addresses to functions in C

cfunctionpointersmemory-address

提问by Amit

I'm new to C and I have a function that calculates a few variables. But for now let's simplify things. What I want is to have a function that "returns" multiple variables. Though as I understand it, you can only return one variable in C. So I was told you can pass the address of a variable and do it that way. This is how far I got and I was wondering I could have a hand. I'm getting a fair bit of errors regarding C90 forbidden stuff etc. I'm almost positive it's my syntax.

我是 C 的新手,我有一个计算几个变量的函数。但是现在让我们简化一下。我想要的是有一个“返回”多个变量的函数。虽然据我所知,你只能在 C 中返回一个变量。所以我被告知你可以传递一个变量的地址并这样做。这是我走了多远,我想知道我能伸出援手。我在 C90 禁止的东西等方面遇到了相当多的错误。我几乎肯定这是我的语法。

Say this is my main function:

说这是我的主要功能:

void func(int*, int*);

int main()
{
    int x, y;
    func(&x, &y);

    printf("Value of x is: %d\n", x);
    printf("Value of y is: %d\n", y);

    return 0;
}

void func(int* x, int* y)
{
    x = 5;
    y = 5;
}

This is essentially the structure that I'm working with. Could anyone give me a hand here?

这基本上是我正在使用的结构。有人可以帮我吗?

回答by Mehrdad Afshari

You should use *variableto refer to what a pointer points to:

您应该使用*variable来指代指针指向的内容:

*x = 5;
*y = 5;

What you are currently doing is to set the pointer to address 5. You may get away with crappy old compilers, but a good compiler will detect a type mismatch in assigning an intto an int*variable and will not let you do it without an explicit cast.

您当前正在做的是将指针设置为地址 5。您可能会使用蹩脚的旧编译器,但是一个好的编译器会在将 赋值intint*变量时检测到类型不匹配,并且不会让您在没有显式转换的情况下执行此操作。

回答by Amit

void function(int *x, int* y) {
    *x = 5; 
    *y = 5; 
}

would change the values of the parameters.

会改变参数的值。

回答by Ingo

You can't forward declare func(int,int) when in reality it is func(int*, int*). Moreover, what should the return type of func be? Since it doesn't use return, I'd suggest using void func(int*, int*).

当实际上它是 func(int*, int*) 时,您不能转发声明 func(int,int)。此外,func的返回类型应该是什么?由于它不使用 return,我建议使用 void func(int*, int*)。

回答by pmg

You can return a single variable of a struct type.

您可以返回结构类型的单个变量。

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

struct Multi {
  int anint;
  double adouble;
  char astring[200];
};

struct Multi fxfoo(int parm) {
  struct Multi retval = {0};
  if (parm != 0) {
    retval.anint = parm;
    retval.adouble = parm;
    retval.astring[0] = parm;
  }
  return retval;
}

int main(void) {
  struct Multi xx;
  if (fxfoo(0).adouble <= 0) printf("ok\n");
  xx = fxfoo(42);
  if (strcmp(xx.astring, "\x2a") == 0) printf("ok\n");
  return 0;
}

回答by jonsca

In addition to the changes that the other posters have suggested for your function body, change your prototype to void func(int *,int *), and change your function definition (beneath main) to reflect void as well. When you don't specify a return type, the compiler thinks you are trying to imply an int return.

除了其他海报为您的函数体建议的更改之外,将您的原型更改为void func(int *,int *),并更改您的函数定义(在 main 之下)以反映 void。当您不指定返回类型时,编译器认为您试图暗示 int 返回。