0

I have been searching for the answer for this for about an hour and I am very new to C. I am getting the warning "left-hand operand of comma expression has no affect" when referring to this line

return studio, one, two;

The statement is not returning variables one or two to my knowledge from what my print function is showing. Is this the correct way to return multiple variables in C or am I doing it wrong? This is the first time I have had to do this and we didn't discuss this in my class. Apologizes in advance for (I'm sure) a very simple fix.

6
  • If this syntax was allowed (which it isn't), how would you call the function? Commented Feb 2, 2015 at 23:44
  • 1
    @500-InternalServerError yes it is, but it discards both first expressions after having evaluated them. Commented Feb 2, 2015 at 23:46
  • @Quentin: Well, allowed, but unrewarding, then :) Commented Feb 2, 2015 at 23:48
  • 1
    @500-InternalServerError that sums up C pretty well :p Commented Feb 2, 2015 at 23:53
  • 1
    1) return structure 2) write to pointers passed in as arguments 3) Allocate/populate a structure and return its pointer. or 4) ugh: global variables. #1 is best. Commented Feb 2, 2015 at 23:56

3 Answers 3

3

You can't return multiple values in C. Your options are to return a struct, or to pass in references to the variables you want to modify:

Return struct:

struct studio_stuff {
   int studio;
   int one;
   int two;
};

struct studio_stuff foo()
{
   struct studio_stuff ret;
   ret.studio = 0;
   ret.one = 1;
   ret.two = 2;
   return ret;
}

Pass in references to modify:

void foo(int * studio, int * one, int * two)
{
    *studio = 0;
    *one = 1;
    *two = 2;
}
Sign up to request clarification or add additional context in comments.

Comments

2

You cannot return multiple values in C. You can achieve a similar (though not identical) effect by one of:

  • returning a structure or (pointer to a conceptual) array.
  • writing to multiple pointers passed as parameters

2 Comments

Note that you can't return an actual array.
Note that if you return a pointer to an array, you have to worry about memory allocation. Embedding the array in a structure avoids that problem, but reduces to the first alternative (return a structure). On the whole, return a structure or multiple pointer arguments are the best choices.
2

The comma operator evaluates its first operand, discards the result, and gives the result of the second.

So return studio, one, two will evaluate studio, discard the value, evaluate one, discard the value, evaluate two and return that value.

There is no way to return multiple values from a function. The options are to return a struct (the value of a struct is composed of a set of values) or pass additional arguments that are pointers. For example;

struct X func();

works because struct X can contain multiple values.

Alternatively

int func(double *y);

is a function that can return an int, or set the value of *y (which will be visible to the caller).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.