How to return two values in a function?

I

istillshine

double *foo(double *x, int *ret)
{
double *x;
int sz;

x = malloc(100);

/* do something on x */

/* compute sz */

ret = malloc(sz);

/* do something on y*/

return x;
}


In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?

struct ret_val_t {
double *x;
int *ret;
}

One solution might be to use a global variable, int *ret. But I think
this is not a good solution since people usually recommend agaist
using global variables.
 
B

Barry Schwarz

double *foo(double *x, int *ret)
{
double *x;

You don't want two definitions of x.
int sz;

x = malloc(100);

/* do something on x */

/* compute sz */

ret = malloc(sz);

/* do something on y*/

return x;
}


In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?

One common method is to change the function definition to
double *foo(double *x, int **ret)
and within the function change ret to *ret.

In the calling function, instead of calling the function with
foo(my_double_pointer, my_int_pointer);
you would use
foo(my_double_pointer, &my_int_pointer)

If you chose the method, I would do it for both arguments and change
the function to return nothing (void) or to return an int success
flag.
struct ret_val_t {
double *x;
int *ret;
}

One solution might be to use a global variable, int *ret. But I think
this is not a good solution since people usually recommend agaist
using global variables.

When you start to write complicated programs with many functions, you
will appreciate this advice.


Remove del for email
 
I

istillshine

When you start to write complicated programs with many functions, you
will appreciate this advice.
Thanks.


Remove del for email

I don't understand this.
 
P

pereges

double *foo(double *x, int *ret)
{
double *x;
int sz;

x = malloc(100);

/* do something on x */

/* compute sz */

ret = malloc(sz);

/* do something on y*/

return x;

}

In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?

struct ret_val_t {
double *x;
int *ret;

}


Try using double pointers. In the calling function don't pass the copy
of the pointer, instead pass the address of the pointer. Your pointer
will get modified.

void foo(double **x, int **ret)
{

}

void calling_function()
{
double *x;
int *ret;

foo(&x, &ret);

}

Also read FAQ 4.8
 
B

Bartc

double *foo(double *x, int *ret)
{
double *x;
int sz;

x = malloc(100);

/* do something on x */

/* compute sz */

ret = malloc(sz);

/* do something on y*/

return x;
}


In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?

The advice you've received has been to return the values in the parameters,
so (to use a much simpler example):

#include<stdio.h>
void returnpair(int *a,int *b) {
*a+=100;
*b+=200;
}

int main(void) {
int a,b;
a=20;
b=30;
returnpair(&a,&b);
printf("A', B' = %d %d\n",a,b);
}

You need an extra level of pointers (hence the ** for your example). However
this doesn't address your question (and will also overwrite your
parameters).
struct ret_val_t {
double *x;
int *ret;
}

This is the natural solution; what's wrong with this?
One solution might be to use a global variable, int *ret. But I think
this is not a good solution since people usually recommend agaist
using global variables.

If it works, use it until you have time to do it better.

Another way is to return the parameters one at a time, but it could mean
calculating twice, depending on how you arrange your function, and you need
an extra parameter to select the return value:

#include<stdio.h>

int returnpair(int a,int b, int n) {
a+=100;
b+=200;
if (n==1) return a;
return b;
}

int main(void) {
int a,b,anew,bnew;

a=20;
b=30;
anew = returnpair(a,b,1); /* must not change a,b between calls */
bnew = returnpair(a,b,2);

printf("A', B' = %d %d\n",anew,bnew);
}
 
J

John Bode

double *foo(double *x, int *ret)
{
double *x;
int sz;

x = malloc(100);

/* do something on x */

/* compute sz */

ret = malloc(sz);

/* do something on y*/

return x;

}

In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?

struct ret_val_t {
double *x;
int *ret;

}

One solution might be to use a global variable, int *ret. But I think
this is not a good solution since people usually recommend agaist
using global variables.

Yeah, you generally do not want to rely on global variables (they
promote tight coupling between modules and make code harder to
reuse).

If the values you are returning naturally make up a composite type
(such as fields in a street address), use a struct to group them
together and return an instance of the struct. If the values you are
returning do not naturally make up a composite type (such as a buffer,
buffer length, and read status), use separate writable function
parameters.
 
K

Kenny McCormack

John Bode said:
Yeah, you generally do not want to rely on global variables (they
promote tight coupling between modules and make code harder to
reuse).

Luckily, since C doesn't have global variables (ask anyone in this
group!), you have nothing to fear here.
 
R

Richard

Luckily, since C doesn't have global variables (ask anyone in this
group!), you have nothing to fear here.

Serious question for you - having read all Heathfield's bluster, is
there any way you could be convinced that C does not have "global
variables"? I know that I shake my head more and more when I read his
bullshit. What I want to know is , what the hell does he and the other
clique members think they are gaining by playing such silly games? I
have worked on more big C projects than I care to remember and not once
did any one get confused when "globals" where mentioned in the context
of the program. Why here in c.l.c? Really. My mind boggles at their self
important posturing in the face of common sense.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top