How can I return more than one value through a function in C?

I was asked in an interview how to return more than one value from a function. I answered by saying, using pointers that we can reach (call by reference) this is in C. Then he told me that he was looking for another way to return more than one value. I said that we can return a struct object, but here it was not impressive either.

I would like to know other ways to return more than one value from a function.

I saw that these questions were asked here on SO, but could not find anything special C.

+7
source share
5 answers

The difficult problem is that the interviewer has some kind of solution in which they are especially satisfied, and they will probably evaluate you if you have the same smart trick as theirs.

You could name a few such ways as you, and yet you do not fall into their secret trick. And if you knew their secret trick, you could not be impressed with it.

So, in these situations, to turn him from an interview into a conversation. Once you find that you are not moving in the direction of your ego, you can avoid turning to the bullying “I don’t know” “I give up” and instead try “so do you have any smart solution? A recipe at home for this in Xyz Inc ? " and etc.

Any views on their clearly presumptuous decision, and you are back on solid ground where you can talk about it and ask them if they thought about the various factors that come to mind and basically interview them.

Everyone loves a good listener, and making them talk about their tricks is a good way to make them leave the interview very impressed with you!;)

+27
source

There are several ways:

  • Return value using the return statement (as you already know)
  • Return via links.
  • Returns values ​​through a heap.
  • Returns values ​​through global variables.
+1
source

It depends on what you consider the value. If a value is part of the information for you, more values ​​may be a value structure. Additional values ​​can also be passed through pointers or arrays, even char* , containing a list of (non-zero alphanumeric) values. If you think the value is a bit of information, one uint32_t returned may contain 32 values. You might even bother with signals or sockets or pipes or files.

But for you I don’t even know which use case and what requirements it imposes on the solution, it is a rather difficult task to come up with the right solution (and you really came up with some right solutions ...).

+1
source

Returns a pointer to a structure or wraps several small data types into one large data type or uses global variables. The first is probably the cleanest way to do this, the other two can use them in certain situations.

If we pass the address instead of the true value of the parameters.
Then, when we refer to these parameters, we do this with the address.

0
source

returning a pointer to a structure is the appropriate answer. (Obviously, the goal of the program may decide what is best to do). Perhaps the interviewer wanted you to say “I don’t know,” which would demonstrate your distrust in this area. I think you provided good solutions, although not what he had in his head. You could ask him about a typical scenario when he wanted to return a few values, and then discuss how a structure pointer is a reasonable alternative.

0
source

All Articles