473,386 Members | 1,743 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,386 software developers and data experts.

how to return multi-value from a function

such as a function: f(double v[], int a)

could this function return both a integer and array?

Thank you very much. I have confused on this for a long time~

Sep 27 '07 #1
5 2005
shanfeng wrote:
such as a function: f(double v[], int a)

could this function return both a integer and array?

Thank you very much. I have confused on this for a long time~
There many ways to do that:

typedef struct {
int a;
int array[21];
} RTYPE;

RTYPE f(double v[],int a);

Another solution is:

int f(double v[], int a, double **pResult);

This function returns an integer and puts the
result array in *pResult.

Yet another solution is:

int f(double v[], int a, int *pintResult,double **pResult);
This function returns an eror code. If the error code is
zero, pintResult will be filled with an integer and
*pResult will contain an array.
And last but not least:

int *f(double v[], int a);
This function returns a pointer to a counted array. The first
element is the length, followed by an array of that length

Sep 27 '07 #2
On Sep 27, 3:09 pm, shanfeng <mir...@gmail.comwrote:
such as a function: f(double v[], int a)

could this function return both a integer and array?

Thank you very much. I have confused on this for a long time~
A function can return at most a single value. That value may be a
pointer to an array of items or to a structure, it could also be a
structure itself. Other options for communicating multiple pieces of
information to the caller include modifying file-scope variables
(a.k.a. global variables, not usually preferable) or passing pointers
to variables to the function and modifying the values of the objects
to which those pointers point.

Robert Gamble

Sep 27 '07 #3
Thank you! You guys are so professional:)
All your replies are so helpful for me,
athough there are some terms I am not very familiar, I will work on
it .

Sep 27 '07 #4
jacob navia wrote:
shanfeng wrote:
>such as a function: f(double v[], int a)

could this function return both a integer and array?

Thank you very much. I have confused on this for a long time~
There many ways to do that:

typedef struct {
int a;
int array[21];
} RTYPE;

RTYPE f(double v[],int a);
I would add

int f(double v[], int a, *RTYPE r);

where f returns a status code (e.g., 0 on success, 1 otherwise) and
fills the r structure upon success.
>
Another solution is:

int f(double v[], int a, double **pResult);

This function returns an integer and puts the
result array in *pResult.

Yet another solution is:

int f(double v[], int a, int *pintResult,double **pResult);
This function returns an eror code. If the error code is
zero, pintResult will be filled with an integer and
*pResult will contain an array.
And last but not least:

int *f(double v[], int a);
This function returns a pointer to a counted array. The first
element is the length, followed by an array of that length

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Sep 28 '07 #5
On Thu, 27 Sep 2007 21:16:43 +0200, jacob navia
<ja***@jacob.remcomp.frwrote:
shanfeng wrote:
such as a function: f(double v[], int a)

could this function return both a integer and array?

Thank you very much. I have confused on this for a long time~
Note that arrays as such are never passed to a function in C, instead
a pointer to the first element of the array is passed and can be used
to access the elements of the array; the effect is the same as passing
the array itself by-reference in some other languages. In this case
f() can store data into the array passed by the caller, if it is large
enough and is not defined as 'const'. That is probably the most
commonly used method in C, and thus usually the one most easily
understood by others reviewing or working on your code.
There many ways to do that:

typedef struct {
int a;
int array[21];
} RTYPE;

RTYPE f(double v[],int a);

Another solution is:

int f(double v[], int a, double **pResult);

This function returns an integer and puts the
result array in *pResult.
You can't put an array in *pResult. You can put a pointer 'to' an
array, or more precisely a pointer to the beginning of an array (of
double). But the space pointed to must be allocated somehow:
+ statically -- but not safely reusable, reentrant, or threadsafe;
+ dynamically -- but caller must take responsibility for free'ing it.
Yet another solution is:

int f(double v[], int a, int *pintResult,double **pResult);
This function returns an eror code. If the error code is
zero, pintResult will be filled with an integer and
*pResult will contain an array.
*pintResult get an int, and *pResult will _point to/at_ array.
>
And last but not least:

int *f(double v[], int a);
This function returns a pointer to a counted array. The first
element is the length, followed by an array of that length
Which again has to be allocated somehow, and only works if the desired
array element type is also int -- OP didn't say, and his only example
data was double.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Oct 8 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
1
by: Mike L | last post by:
This is for a Win form. I am able to send a string to my multi line text box, but I want the string to have hard returns in it. Here is my code for the string. string s =...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
7
by: ashu | last post by:
look at code #include<stdio.h> int *mult(void); int main(void) { int *ptr,i; ptr=mult; for(i=0;i<6;i++) { printf("%d",*(ptr++));
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
5
by: downwitch | last post by:
I have the strangest error, and wonder if anyone else has seen it. I'm using ADODB to return a set of SQL Server 2005 records via ODBC. If I open the recordset this way With cmd .CommandType =...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.