443,903 Members | 1,977 Online Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

# how to return multi-value from a function

 P: n/a 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 Replies

 P: n/a 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; } 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

 P: n/a On Sep 27, 3:09 pm, shanfeng

 P: n/a 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

 P: n/a 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; } 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

 P: n/a On Thu, 27 Sep 2007 21:16:43 +0200, jacob navia 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 discussion thread is closed

Replies have been disabled for this discussion. 