Need 2 values from function

J

JD

Hi guys

What if I need to return two values from a function. What's the best way
to do it?

Function is to load a text file into memory like so:

-----8<-----

char *LoadFile(char *infile)
{
FILE *f;
int c;
int bytesread, bytestotal;
char *buf, *p, *tmp;

if ((f = fopen(infile, "r")) == NULL) {
printf("Can't open \"%s\" for reading\n", infile);
return NULL; /* NULL means failure to calling function */
}

bytesread = 0;
bytestotal = 0;
buf = NULL;
while ((c = fgetc(f)) != EOF) {
if (bytestotal - bytesread == 0) {
bytestotal += 1024;
tmp = realloc(buf, bytestotal);
if (tmp == NULL) {
printf("*** Out of memory ***\n");
return NULL;
}
buf = tmp;
p = buf + bytesread;
}
*p++ = c;
bytesread++;
}

*p = '\0'; /* terminate string...could this be out of bounds in
certain situations? */

fclose(f);
return buf;
}

----->8-----

I need to return the pointer to the memory as well as "bytestotal" so I
can allocate another block of memory for working with the data (copying
it back and forth to do operations on it etc.) It seems inefficient to
call the function twice so I have two blocks of memory.

I'm aware of some possibilities: use "call by reference", return a
struct, use a global variable. I'm just not sure which to choose and why.

Thanks!
 
I

Ico

JD said:
Hi guys

What if I need to return two values from a function. What's the best way
to do it?

A common way is to pass pointers to your function for every return
variable, which contents are changed from within the function.

For example : (code not tested)

void somefunc(int a, int *b, int *c)
{
*b = a * 2;
*c = a * 3;
}

You could call this with

int value = 10;
int timestwo;
int timesthree;

somefunc(value, &timestwo, &timesthree);

printf("%d %d\n", timestwo, timesthree);


If you have to return a lot of values, more the four or five or so, it
might be preferrable to create a struct for passing the variables, like
so :

struct stuff {
int timestwo;
int timesthree;
int timesfour;
int timesfive;
};

void somefunc(int a, struct stuff *data) {
data->timestwo = a * 2;
data->timesthree = a * 3;
data->timesfour = a * 4;
data->timesfive = a * 5;
}

and call it like


int value = 10;
struct data;

somefunc(a, &data);

printf("%d %d %d %d\n", data.timestwo, data.timesthree,
data.timesfour, data.timesfive);



[ snipped code example ]
I need to return the pointer to the memory as well as "bytestotal" so I
can allocate another block of memory for working with the data (copying
it back and forth to do operations on it etc.) It seems inefficient to
call the function twice so I have two blocks of memory.

I'm aware of some possibilities: use "call by reference", return a
struct, use a global variable. I'm just not sure which to choose and why.

'Call by reference' is not part of the C language. The pointer method I
described above can be used to accomplish similar things.

Global variables are usually considered a bad thing; if you can avoid
it, do not use them.

Ico
 
P

pete

JD said:
Hi guys

What if I need to return two values from a function.
What's the best way to do it?

Function is to load a text file into memory like so:

-----8<-----

char *LoadFile(char *infile)
{
FILE *f;
int c;
int bytesread, bytestotal;
I'm aware of some possibilities: use "call by reference",

That's OK, if this is what you mean:

char *LoadFile(char *infile, int *bytestotal);
 
M

Mark McIntyre

Hi guys

What if I need to return two values from a function. What's the best way
to do it?

either use pointers
void modifies_a_and_b( char* a, int* b);
or return a struct

struct twovals {char a; int b; };
struct twovals fn_returns_struct(void);
I'm aware of some possibilities: use "call by reference",

Aint no such thing in C - all calls are by value. You can pass a
pointer in, as above.
return a struct, use a global variable.

The last is a bad idea. Don't use globals if at all possible.
I'm just not sure which to choose and why.

It totally depends on what you do with the data.
Mark McIntyre
 
I

Ian Collins

JD said:
Hi guys

What if I need to return two values from a function. What's the best way
to do it?
There is no fixed rules as to whether returning a struct containing the
values, or passing pointers to the result as parameters to the function
is best.

Which you use is dependant on the types you are returning and the
machine you are using. For example if your return values as a struct
fit a machine register, it may be best to return a struct.

Play and see which works best for you in your situation.
 
S

serrand

JD said:
Hi guys

What if I need to return two values from a function. What's the best way
to do it?

Function is to load a text file into memory like so:

-----8<-----

char *LoadFile(char *infile)
{
FILE *f;
int c;
int bytesread, bytestotal;
char *buf, *p, *tmp;

if ((f = fopen(infile, "r")) == NULL) {
printf("Can't open \"%s\" for reading\n", infile);
return NULL; /* NULL means failure to calling function */
}

bytesread = 0;
bytestotal = 0;
buf = NULL;
while ((c = fgetc(f)) != EOF) {
if (bytestotal - bytesread == 0) {
bytestotal += 1024;
tmp = realloc(buf, bytestotal);
if (tmp == NULL) {
printf("*** Out of memory ***\n");
return NULL;
}
buf = tmp;
p = buf + bytesread;
}
*p++ = c;
bytesread++;
}

*p = '\0'; /* terminate string...could this be out of bounds in
certain situations? */

fclose(f);
return buf;
}

----->8-----

I need to return the pointer to the memory as well as "bytestotal" so I
can allocate another block of memory for working with the data (copying
it back and forth to do operations on it etc.) It seems inefficient to
call the function twice so I have two blocks of memory.

I'm aware of some possibilities: use "call by reference", return a
struct, use a global variable. I'm just not sure which to choose and why.

Thanks!


The simpliest is the best in most cases ;-)

use the return for the bytestotal : then if error occurs yoy can return -1 for example...
use "call by ref" for the buffer

==> then you should verify buf == NULL first ...

xavier
 
G

Gregory Pietsch

JD said:
Hi guys

What if I need to return two values from a function. What's the best way
to do it?

Function is to load a text file into memory like so:

I need to return the pointer to the memory as well as "bytestotal" so I
can allocate another block of memory for working with the data (copying
it back and forth to do operations on it etc.) It seems inefficient to
call the function twice so I have two blocks of memory.

I'm aware of some possibilities: use "call by reference", return a
struct, use a global variable. I'm just not sure which to choose and why.

Thanks!

This looks like yet another job for FreeDOS Edlin! Grab it from ibiblio
or alt.sources and you can see for yourself how it manipulates buffers.

Gregory Pietsch
 
P

Paul Connolly

structures are passed by value (i.e. they are (shallow) copied)
so I'd suggest something along the lines of:
struct ret_vals { int ret_val1; int ret_val2; };

struct ret_vals
my_func( int input_param )
{
struct ret_vals = { 0 };
/* do your stuff*/
ret_vals.ret_val1 = 0 /*or something from your stuff*/;
trt_vals.ret_val2 = 0 /*or something from your stuff*/;
return ret_vals;
}

/* if there are many values to return and you are concerned about
efficiency
then consider passing in a pointer to a struct retvals
*/
Ico said:
JD said:
Hi guys

What if I need to return two values from a function. What's the best way
to do it?

A common way is to pass pointers to your function for every return
variable, which contents are changed from within the function.

For example : (code not tested)

void somefunc(int a, int *b, int *c)
{
*b = a * 2;
*c = a * 3;
}

You could call this with

int value = 10;
int timestwo;
int timesthree;

somefunc(value, &timestwo, &timesthree);

printf("%d %d\n", timestwo, timesthree);


If you have to return a lot of values, more the four or five or so, it
might be preferrable to create a struct for passing the variables, like
so :

struct stuff {
int timestwo;
int timesthree;
int timesfour;
int timesfive;
};

void somefunc(int a, struct stuff *data) {
data->timestwo = a * 2;
data->timesthree = a * 3;
data->timesfour = a * 4;
data->timesfive = a * 5;
}

and call it like


int value = 10;
struct data;

somefunc(a, &data);

printf("%d %d %d %d\n", data.timestwo, data.timesthree,
data.timesfour, data.timesfive);



[ snipped code example ]
I need to return the pointer to the memory as well as "bytestotal" so I
can allocate another block of memory for working with the data (copying
it back and forth to do operations on it etc.) It seems inefficient to
call the function twice so I have two blocks of memory.

I'm aware of some possibilities: use "call by reference", return a
struct, use a global variable. I'm just not sure which to choose and why.

'Call by reference' is not part of the C language. The pointer method I
described above can be used to accomplish similar things.

Global variables are usually considered a bad thing; if you can avoid
it, do not use them.

Ico
 
C

Chris Torek

(I realize this article is somewhat old; I have been saving it
to reply to, while hoping someone else would point this out first.)

What if I need to return two values from a function. What's the best way
to do it?

Lots of people answered that part just fine.
char *LoadFile(char *infile)
{
FILE *f;
int c;
int bytesread, bytestotal;
char *buf, *p, *tmp;

if ((f = fopen(infile, "r")) == NULL) {
printf("Can't open \"%s\" for reading\n", infile);
return NULL; /* NULL means failure to calling function */
}

bytesread = 0;
bytestotal = 0;
buf = NULL;
while ((c = fgetc(f)) != EOF) {
[snip code that expands the buffer in units of 1024 chars at a time]
}

*p = '\0'; /* terminate string...could this be out of bounds in
certain situations? */

The answer to the question in the comment is "yes". In fact, it
should now be obvious that "p" will be invalid in at least one
case. What happens if the very first fgetc() returns EOF?

(In fact, writing on *p is invalid whenever the size of the file
is congruent to zero mod 1024, given the snipped code. A string
of length N requires N+1 bytes of storage.)
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top