By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

How to get more than one value from function in c

P: 2
Hi,

I want a function in c than can return more than one integer values in array passed as argument.

From main function i m calling a function (say foo)with array as argument. I want foo to return set of values in array which was passed as arg. foo should allocate memory for each array element.

please help me.

Kiran.
Sep 22 '10 #1
Share this Question
Share on Google+
5 Replies


100+
P: 175
I believe what you are looking for is passing by reference. You need to pass the array into the function by REFERENCE, instead of by VALUE. Basically, this allows you to change the array passed into a function, which when returning to Main(), the array you originally passed into the function will be updated. This works because when you pass by reference, you're working directly with the array in memory, instead of a copy of it. You can't return multiple values from a function. But when you want to, this is how it's done.

The other option is making the array global, but most people frown on that.
Sep 22 '10 #2

100+
P: 207
Aren't all arrays passed by reference in C?

By Kirans original statement, foo should allocate memory for each arry element, I believe he is looking for more of a pointer to a pointer or a reference to a pointer so that he can pass a pointer in and what it is pointing at will be changed at the end of the function. Somthing like this could work.

void foo(int * & ptr, int & numberOfElements)

numberOfElements tracks how many elements where created inside the function so you don't go out of bounds later.
Sep 22 '10 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
The question is not making sense.

Arrays are passed to functions typically by using the array name. The array name is aleays the address of element 0.

Therefore, when you update array values inside the function you are actually updatuing the array that's outside the function.

As to returning multiple values (assuming using the return keyword), you must return a type ot a pointer to a type. Therfore, multiple returned values need to be in a struct or union (or if C++ struct, union or class).
Sep 22 '10 #4

100+
P: 542
Here is a function which returns more than one value.
Expand|Select|Wrap|Line Numbers
  1. void computeCircle(double& area, double& circumference,double r)
  2. //returns the area and circumference of a circle wih
  3. //radius r
  4. {
  5. area = PI*r*r;
  6. circumference = 2*PI*r;
  7. }
Sep 23 '10 #5

P: 2
Hi all,

Thanks for your help. I got what i wanted.

Below is a snippet:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int main()
  3. {
  4.  int b, num;
  5.  int *array;
  6.  
  7.  b=func(&array, &num);
  8.  
  9.  for (b=0; b<=num; b++)
  10.   {
  11.     printf("%d\n", array[b]);
  12.   }
  13. return 0;
  14. }
  15.  
  16. int func (int **array, int *num)
  17. {
  18. int i;
  19. int *p;
  20. for (i=0; i<4; i++)
  21. {
  22.     p = (int *)realloc(p,(i * sizeof(int)));
  23.     p[i] = i+5;
  24. }
  25.  
  26. //Return address of constructed array
  27. *array=p;
  28.  
  29. //Return no of elements to ensure we r not crossing over
  30. *num=i-1;
  31.  
  32. return 0;
  33. }
  34.  
  35.  
Sep 23 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.