Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

I have been asked in an interview how one can return more than one value from function. I have answered saying by using pointers we can achieve(call by reference) this in C. Then he told me he is looking for some other way of returning more than one value. I said we can return a struct object but here also he didn't seem to be impressed.

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

I have seen this questions being asked here on SO, but could not find anything C specific.

like image 799
Amit Singh Tomar Avatar asked Nov 24 '11 07:11

Amit Singh Tomar


2 Answers

The tricky problem is that the interviewer has some solution they are particularly happy with in mind and they are likely grading you by whether you have the same clever trick as them or not.

You could just name a few ways such as you did, and still not fall upon their secret trick. And if you knew their secret trick, you could well not be impressed with it.

So in these situations, its to turn it from interview into conversation. Once you detect you're not moving towards their ego, you can avoid heading towards the intimidating "I don't know" "I give up" and instead try out the "so do you have any clever solution? Is there an in-house recipe for this at Xyz Inc?" etc.

Any glimpse at their obviously self-impressed solution and you are back on firm ground where you can talk about it and ask them if they have thought about various factors that come to mind and basically interview them.

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

like image 62
Will Avatar answered Nov 17 '22 21:11

Will


There are a few ways:

  1. Return value using the return statement (as you already know)
  2. Return via references.
  3. Return values via the heap.
  4. Return values via global variables.
like image 30
MByD Avatar answered Nov 17 '22 21:11

MByD