Question:
How can I return multiple values in C programming using local variables?
2013-12-31 00:51:26 UTC
We had an exam question about this. And I'm not sure about global and local variables. I know I can use pointers or structs in returning multiple values.

Let's say

struct name{
//somecode
} s;

this makes it a global variable right?

but if I declare it inside the main() or any other function

main{
struct name s1;
//somecode
}

is this is acceptable?
Four answers:
?
2013-12-31 14:37:10 UTC
You don't require global scope variables for this. And I don't believe the question wanted a global variable answer. Nor do I imagine they wanted a pointer answer. I suspect they wanted something like this:



struct reduction { unsigned int n, c; };

struct reduction reduce( unsigned int n, unsigned int q ) {

    struct reduction result;

    result.c= 0;

    while ( (n % q) == 0 )

        n /= q, ++result.c;

    result.n= n;

    return result;

}



The function reduce() accepts a value n and a possible factor q and returns the "reduced" value, once the factors have been removed, and also the number of times that factor was found (essentially, the power of the factor.)



Note that a structure is used as a local variable, as suggested by the question, and that multiple values can be returned that way.
Sadsongs
2013-12-31 01:02:25 UTC
It's global if the variable is declared outside all functions. Otherwise, it's local to the function in which it's declared.



You have defined the structure but that doesn't declare a variable - there's nothing allocated until you get to main. If you moved "struct name s1;" above main, then s1 would be global.
justme
2013-12-31 06:41:50 UTC
In your example code the struct "s" is global but the one you declare in your main "s1" is local.



How do you return multiple variables using a local variable? pass a pointer to the struct to your function.



struct name{

//somevariables

};



function(name* n);



main

{

struct name n1;



function(&n1);

}



function(name* n)

{

name->//somevariable = something

}
James Bond
2013-12-31 01:29:34 UTC
void xxx(.....,int res[])

{

.....

....

res[0]=thevaluetobereturned

res[1]=anothervaluetobereturned...

}

When we call this function, the argument what ever you pass in place of res will have the values what you want to return





int * xxx(....)

{

int *res=(int *)malloc(...);

...

...



res[0]=thevaluetobereturned

res[1]=anothervaluetobereturned...



return res;

}

Here, you are creating a dynamic array and storing what ever values you want to return. Then, we return the address of that array.



void xxx(...., int *a, int *b)

{

............

...........

*a=thevaluetobereturned

*b=anothervaluetobereturned...

}

main()

{

int p, q;



xxx(...., &p,&q);



}

Here, p, q contains two values that are calculated in function xxx. One can taken any number of addresses.I haveshown only two.



Global variables thing you have mentioned already

Also, about structures you have mentioned.



Like this we can return more than one value from a function


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...