开发者

Passing and returning variables in C

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.

How would I do that? Would I just return the 3 values from FuncA and just pass it in as parameter of 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"开发者_StackOverflow社区 &A, &B, &C);
    return A, B, C;
}

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


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

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 return keyword. It's assigning values at a specific location in memory which is, A, B, and 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:

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)
{
    // ...
}


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


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)
{
    //...
}


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.

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


Declare A, B and C as global variables:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜