C语言 在 C 中传递和返回变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5130978/
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 07:54:32  来源:igfitidea点击:

Passing and returning variables in C

cparameter-passing

提问by Thao Nguyen

I'm trying to pass variables from one function to another.

我试图将变量从一个函数传递到另一个函数。

Like for example:

例如:

FuncA: takes in 3 inputs from the user and I want to use those 3 inputs in FuncB.

FuncA:接受用户的 3 个输入,我想在 FuncB 中使用这 3 个输入。

How would I do that? Would I just return the 3 values from FuncA and just pass it in as parameter of Func B?

我该怎么做?我是否只从 FuncA 返回 3 个值并将其作为 Func B 的参数传入?

Would I do something like this? **Without using pointers.

我会做这样的事情吗?**不使用指针。

int FuncA(void);
int FuncB(int A, int B, int C, int D, int E);

int main(void)
{
    FuncA(void);
    FuncB(A,B,C);
}

int FuncA(void)
{
    printf("Enter 3 number:");
    scanf("%d %d %d" &A, &B, &C);
    return A, B, C;
}

int FuncB(int A, int B, int C)
{
    .............
}

回答by Marlon

Firstly, you can only returnone value per function. This will probably make you ask, "how is it possible to get the values for A, B, and C from FuncA?"

首先,return每个函数只能有一个值。这可能会让您问,“如何从 FuncA 中获取 A、B 和 C 的值?”

How much do you know about pointers? The solution will be difficult to understand if you do not have a firm grip of what pointers are and how they work.

你对指针了解多少?如果您没有牢牢掌握指针是什么以及它们是如何工作的,那么解决方案将很难理解。

The solution is to pass 3 pointers (one for A, B, and C) so that FuncA can assign a value to them. This doesn't use the returnkeyword. It's assigning values at a specific location in memory which is, A, B, and C.

解决方案是传递 3 个指针(一个用于 A、B 和 C),以便 FuncA 可以为它们赋值。这不使用return关键字。它在内存中的特定位置分配值,即 A、B 和 C。

int FuncA(int* A, int* B, int* C)
{
    printf("Enter 3 number:");
    scanf("%d %d %d", A, B, C);
}

Now that A, B, and C contain the user input, we can pass those values to FuncB. You final code should look like this:

现在 A、B 和 C 包含用户输入,我们可以将这些值传递给 FuncB。您的最终代码应如下所示:

int FuncA(int* A, int* B, int *C);
int FuncB(int A, int B, int C);

int main(void)
{
    int A;
    int B;
    int C;

    FuncA(&A, &B, &C);
    FuncB(A, B, C);
}

int FuncA(int* A, int* B, int* C)
{
    printf("Enter 3 number:");
    scanf("%d %d %d", A, B, C);
}

int FuncB(int A, int B, int C)
{
    // ...
}

回答by Jim Balter

One approach:

一种方法:

typedef struct {
  int a;
  int b;
  int c;
} ABC;

ABC funcA(void);
{
    ABC abc;
    printf("Enter 3 numbers: ");
    fflush(stdout);
    scanf("%d %d %d", &abc.a, &abc.b, &abc.c);
    return abc;
}

void funcB1(ABC abc)
{
    ...
}

void funcB2(int a, int b, int c)
{
    ...
}

int main(void)
{
    funcB1(funcA());  // one alternative

    ABC result = funcA();  // another alternative
    funcB2(result.a, result.b, result.c);
    ...
}

回答by Tim Cooper

I would setup your system like this:

我会像这样设置你的系统:

void FuncA(int *A, int *B, int *C);
int FuncB(int A, int B, int C);

int main(void)
{
  // Declare your variables here
  int A, B, C;
  // Pass the addresses of the above variables to FuncA
  FuncA(&A, &B, &C);
  // Pass the values to FuncB
  FuncB(A, B, C);
}

void FuncA(int *A, int *B, int *C)
{ 
  printf("Enter 3 numbers: ");
  fflush(stdout);
  scanf("%d %d %d", A, B, C);
}

int FuncB(int A, int B, int C)
{
    //...
}

回答by Bala R

FuncA is returning an int. Assuming you want to call FuncB with A,B,C parametersa and return to the caller of FuncA whatever FuncB returns, you want something like this.

FuncA 返回一个整数。假设您想使用 A、B、C 参数 a 调用 FuncB 并返回到 FuncA 的调用者,无论 FuncB 返回什么,您都需要这样的东西。

int FuncA(void)
{ 
printf("Enter 3 number:");
scanf("%d %d %d" &A, &B, &C);
return FuncB(A, B, C);
}

回答by hotpaw2

Declare A, B and C as global variables:

将 A、B 和 C 声明为全局变量:

int A, B, C;
int FuncA(void);
int FuncB(int A, int B, int C);
....

and access them from any function, whether parameters or not. Or declare them static globals to limit the possible damage of global scoping.

并从任何函数访问它们,无论是否有参数。或者将它们声明为静态全局变量以限制全局范围可能造成的损害。