Multiple returns

R

Richard Hunt

What is the best way to get two values back from a function?

I am working through 'The C Programming Language', but I felt
like taking a bit of time off to write another program.

The program has global variables:
extern FILE *fp;
extern Entry *start;

Entry is defined by:

typedef struct entry {
char title[160];
char author[40];
char isbn[14];
struct entry *prev;
struct entry *next;
} Entry;

This program has a function
void createfile()
{
char filename[100];

if(fp!=NULL)
closefile(fp,start);

start=malloc(sizeof(*start));

strcpy(start->author,"AAA");
strcpy(start->title,"AAA");
strcpy(start->isbn,"0-00-000000-0");
start->prev=NULL;
start->next=NULL;

printf("Enter Filename: ");
getline(filename,100);

fp=fopen(filename,"w");
}

getline is pretty much as in k&r. closefile is a function
that steps through the linked list, and frees as it goes,
and then closes the file.

I am aware that the filename is not checked before it is
used, this is typed from memory, and I missed out a bit
there. When I finally get internet access on my computer,
I should be able to improve my questions. :)

If I wanted to do this without using the global variables,
what would be the best way to allow the function to modify
both values, assuming that they are initialized in the
parent function?

btw, the complete program is a simple book catalogue program,
I wrote it to practise linked lists, and to write a checkisbn
function. It stores the entries in the linked list in
alphabetical order by author.

This function is the start of my attempt to add file I/O to
store the catalogue. I have not done this before, except for
messing around with graphics functions that output to xpm, so
I want to try to do it properly.

Richard Hunt

(sorry if the formatting is bad---I'm trying to write this in
Google Groups)
 
L

lallous

Richard Hunt said:
What is the best way to get two values back from a function?

I am working through 'The C Programming Language', but I felt
like taking a bit of time off to write another program.

The program has global variables:
extern FILE *fp;
extern Entry *start;

Entry is defined by:

typedef struct entry {
char title[160];
char author[40];
char isbn[14];
struct entry *prev;
struct entry *next;
} Entry;

This program has a function
void createfile()
{
char filename[100];

if(fp!=NULL)
closefile(fp,start);

start=malloc(sizeof(*start));

strcpy(start->author,"AAA");
strcpy(start->title,"AAA");
strcpy(start->isbn,"0-00-000000-0");
start->prev=NULL;
start->next=NULL;

printf("Enter Filename: ");
getline(filename,100);

fp=fopen(filename,"w");
}

getline is pretty much as in k&r. closefile is a function
that steps through the linked list, and frees as it goes,
and then closes the file.

I am aware that the filename is not checked before it is
used, this is typed from memory, and I missed out a bit
there. When I finally get internet access on my computer,
I should be able to improve my questions. :)

If I wanted to do this without using the global variables,
what would be the best way to allow the function to modify
both values, assuming that they are initialized in the
parent function?

btw, the complete program is a simple book catalogue program,
I wrote it to practise linked lists, and to write a checkisbn
function. It stores the entries in the linked list in
alphabetical order by author.

This function is the start of my attempt to add file I/O to
store the catalogue. I have not done this before, except for
messing around with graphics functions that output to xpm, so
I want to try to do it properly.

Richard Hunt

(sorry if the formatting is bad---I'm trying to write this in
Google Groups)

Hello,

You can return multiple values from a function by:
1)returning a struct
2)passing parameters by reference

As for second solution, it can be done as:
void createfile()
would become:
void createfile(FILE **fp, Entry **start)
{
/* then you would access each of 'fp' and 'start' as:
*fp = fopen(....)

strcpy(*start->title, "....");

*/
}

HTH,
Elias
 
R

Richard Heathfield

lallous said:
Richard Hunt said:
What is the best way to get two values back from a function?

I am working through 'The C Programming Language', but I felt
like taking a bit of time off to write another program.

The program has global variables:
extern FILE *fp;
extern Entry *start;

Entry is defined by:

typedef struct entry {
char title[160];
char author[40];
char isbn[14];
struct entry *prev;
struct entry *next;
} Entry;

This program has a function
void createfile()
{
char filename[100];

if(fp!=NULL)
closefile(fp,start);

start=malloc(sizeof(*start));

strcpy(start->author,"AAA");
strcpy(start->title,"AAA");
strcpy(start->isbn,"0-00-000000-0");
start->prev=NULL;
start->next=NULL;

printf("Enter Filename: ");
getline(filename,100);

fp=fopen(filename,"w");
}

getline is pretty much as in k&r. closefile is a function
that steps through the linked list, and frees as it goes,
and then closes the file.

I am aware that the filename is not checked before it is
used, this is typed from memory, and I missed out a bit
there. When I finally get internet access on my computer,
I should be able to improve my questions. :)

If I wanted to do this without using the global variables,
what would be the best way to allow the function to modify
both values, assuming that they are initialized in the
parent function?

btw, the complete program is a simple book catalogue program,
I wrote it to practise linked lists, and to write a checkisbn
function. It stores the entries in the linked list in
alphabetical order by author.

This function is the start of my attempt to add file I/O to
store the catalogue. I have not done this before, except for
messing around with graphics functions that output to xpm, so
I want to try to do it properly.

Richard Hunt

(sorry if the formatting is bad---I'm trying to write this in
Google Groups)

Hello,

You can return multiple values from a function

No, you can't, but you can fake it...
by:
1)returning a struct

A struct has a single value, comprising the values of all its members. So
yes, this is one way to fake it.
2)passing parameters by reference

Since C doesn't support passing parameters by reference, this isn't really a
true way to describe the second "fake it" method.
As for second solution, it can be done as:

would become:
void createfile(FILE **fp, Entry **start)
{

This passes a FILE ** by value, and an Entry ** by value. Fortunately, a
copy of a pointer is just as good as the original pointer when it comes to
pointing at stuff, so this "fake it" method works too.

Both your suggestions were useful - it's just your terminology that was a
bit slack. :)
 
B

Barry Schwarz

What is the best way to get two values back from a function?

I am working through 'The C Programming Language', but I felt
like taking a bit of time off to write another program.

The program has global variables:
extern FILE *fp;
extern Entry *start;

Entry is defined by:

typedef struct entry {
char title[160];
char author[40];
char isbn[14];
struct entry *prev;
struct entry *next;
} Entry;

This program has a function
void createfile()
{
char filename[100];

if(fp!=NULL)
closefile(fp,start);

start=malloc(sizeof(*start));

strcpy(start->author,"AAA");
strcpy(start->title,"AAA");
strcpy(start->isbn,"0-00-000000-0");
start->prev=NULL;
start->next=NULL;

printf("Enter Filename: ");
getline(filename,100);

fp=fopen(filename,"w");
}
snip
If I wanted to do this without using the global variables,
what would be the best way to allow the function to modify
both values, assuming that they are initialized in the
parent function?
Obviously a function can return only a single value. (If the value is
the aggregate value of a structure then it could have multiple members
but we really don't want to go there.) In order for the function to
update multiple objects in the calling program, the function must know
where those objects are. This is accomplished via pointers.

In the calling function, define the objects createfile() is to update:
FILE *fp = NULL;
Entry *start = NULL;

Call createfile passing the address of each object
createfile(&fp, &start);

The prototype and the function header for createfile() would indicate
that it is receiving pointers to the objects
void createfile(FILE**, Entry**);
and
void createfile(FILE **file, Entry **list){

Every place in your original function where you used the global object
directly, you would now dereference the pointer to the local object in
main. For example, instead of
if(fp!=NULL)
closefile(fp,start);
start=malloc(sizeof(*start));
you would use
if(*file != NULL)
closefile(*file, *list);
*list = malloc(sizeof **list);


<<Remove the del for email>>
 
C

CBFalconer

Richard said:
.... snip ...

typedef struct entry {
char title[160];
char author[40];
char isbn[14];
struct entry *prev;
struct entry *next;
} Entry;
.... snip ...

This function is the start of my attempt to add file I/O to
store the catalogue. I have not done this before, except for
messing around with graphics functions that output to xpm, so
I want to try to do it properly.

You will not be able to store (or restore from) the links prev and
next in a file. Instead you have to make them implicit, or simply
store the structures contents one by one in a file. This is not a
serious problem when you have a simple linked list, but if you
start handling trees, or anything that makes searching easy, you
will need to worry about it.
 
A

Arthur J. O'Dwyer

Richard said:
typedef struct entry {
char title[160];
char author[40];
char isbn[14];
struct entry *prev;
struct entry *next;
} Entry;
... snip ...

This function is the start of my attempt to add file I/O to
store the catalogue. I have not done this before, except for
messing around with graphics functions that output to xpm, so
I want to try to do it properly.

You will not be able to store (or restore from) the links prev and
next in a file. Instead you have to make them implicit, or simply
store the structures contents one by one in a file. This is not a
serious problem when you have a simple linked list, but if you
start handling trees, or anything that makes searching easy, you
will need to worry about it.

I *so* need to start working seriously on 'savestruct' again...
I got it to the point where it parsed struct definitions, and then
stupidly started working on making the output look pretty, rather
than thinking about what sorts of structure it would need to work
with lists/trees/graphs et al. Lack of discipline, that's what it
is... :)
Anyway, <on-topic> good point: writing complex data structures
to files is messy. </on-topic> :)

-Arthur
 
R

Richard Hunt

CBFalconer said:
Richard said:
... snip ...

typedef struct entry {
char title[160];
char author[40];
char isbn[14];
struct entry *prev;
struct entry *next;
} Entry;
... snip ...

This function is the start of my attempt to add file I/O to
store the catalogue. I have not done this before, except for
messing around with graphics functions that output to xpm, so
I want to try to do it properly.

You will not be able to store (or restore from) the links prev and
next in a file. Instead you have to make them implicit, or simply
store the structures contents one by one in a file. This is not a
serious problem when you have a simple linked list, but if you
start handling trees, or anything that makes searching easy, you
will need to worry about it.

Thanks for your help everybody.

What I was planning to do (I have been taking a couple of days off
from programming) was just read through the linked list and just do:

fprintf(fp,"%s\t%s\t%s\n",title,author,isbn);

for each entry. I assume this will work fine?

and to read in I just do the fscanf version of it?

thanks, Richard
 
R

Richard Hunt

I've just remembered that if I use:

char title[160],author[40],isbn[14];

fscanf("%s\t%s\t%s\n",title,author,isbn);

then if any of the strings contain whitespace, this won't work.
What do I do if I only want the formatting that I give to matter?

Richard
 
M

Mark McIntyre

On 24 Jan 2004 05:12:32 -0800, in comp.lang.c ,
What is the best way to get two values back from a function?

define "best".

return a struct if containin the 2 values.
struct twovalues TheFunction(args);

pass in two pointers to data I want to be updated
void TheFunction(int* anintarg, char* achararg);
If I wanted to do this without using the global variables,
what would be the best way to allow the function to modify
both values, assuming that they are initialized in the
parent function?

pass in pointers to them. Works for me.
 
D

Dave Thompson

I've just remembered that if I use:

char title[160],author[40],isbn[14];

fscanf("%s\t%s\t%s\n",title,author,isbn);
That can't be right; the first argument to fscanf must be a "stream",
specifically a pointer to FILE (returned by successful fopen, or stdin
or similar). Also, unlimited %s (or %[) in *scanf is a security hole,
just like gets(); better to use e.g. %159s. But see next.
then if any of the strings contain whitespace, this won't work.
What do I do if I only want the formatting that I give to matter?
I'm not sure what "the formatting that [you] give" means. If you mean
you wrote out the data items separated by tab characters and ended by
a newline, perhaps by using >with *printf< the format string you show,
and assuming the data items never contain tab or newline:
n = fscanf (fp, "%159[^\t]%*c%39[^\t]%*c%13[^\n]%*c", <same>)
if( n != 3 ){ <error> }

or if you want to verify that the delimiters are present within the
expected lengths i.e. no attempted overruns:
"%159[^\t]%*1[\t]%39[^\t]%*1[\t]%13[^\n]%1[\n]"
with an additional ,&dummychar and expect count 4.

If it is possible that some tabs are missing (e.g. manually entered or
edited) and you want newline to override so you don't get out of sync,
make the first two (real, in the second variant) classes [^\n\t].

- David.Thompson1 at worldnet.att.net
 

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
474,139
Messages
2,570,806
Members
47,352
Latest member
Maricruz09

Latest Threads

Top