473,382 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to get more than one value from function in c

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
5 1917
manontheedge
175 100+
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
hype261
207 100+
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
9,208 Expert Mod 8TB
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
whodgson
542 512MB
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
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

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

Similar topics

3
by: Craig | last post by:
I currently have the following code: <input name="castStation" type="text" value="0" onChange="valueCalculate()";> The function called does a few caluclations and writes the total to a total...
1
by: Shanan Worley | last post by:
This is my first course in VB using the Visual Studio.NET 2003. Here is my question. I have a text box that I want a user to enter a dollar value in. I want to use the value in that box and this...
9
by: brian4cards | last post by:
http://www.igearonline.com/subscribe/admin.asp I created a table that is filled with database records. On each row of the table I have a delete submit button to delete that record. I need to...
3
by: ehabaziz2001 | last post by:
I know one of the pointer benefit is that we can return more than one value from a function . The down program has an error that I can not discover . That is the call of the function : The Call:...
43
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions...
6
by: Acrobatic | last post by:
Hello, I know this will be an easy fix--but as of now I'm banging my head against the wall. I need a fresh perspective from the group to see what my problem is: This is a simple accounting...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.