Re: Multiple returns

From: Barry Schwarz (schwarzb_at_deloz.net)
Date: 01/25/04


Date: 25 Jan 2004 03:30:35 GMT

On 24 Jan 2004 05:12:32 -0800, 0102806h@student.gla.ac.uk (Richard
Hunt) wrote:

>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>>



Relevant Pages

  • Re: Two threads reading from same file?
    ... > the open inside the thread function. ... > mutex locks around the fseek/freadcalls. ... to fopenresult in two calls to CreateFile(), which means two handles, ... which means two seek pointers. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: freeing the handle,using CloseHandle.
    ... by CreateFile API.If I need to close handle by CloseHandle API.How to ... make coding errors, ... Given the very, very bad technique of using global variables here, you could do ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: freeing the handle,using CloseHandle.
    ... by CreateFile API.If I need to close handle by CloseHandle API.How to ... make coding errors, ... Given the very, very bad technique of using global variables here, you could do ... MVP Tips:http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: Device driver load sequence
    ... If XXX_Init has not been called when an application does a CreateFile, ... Does the call fail or does it cause the XXX_Init to get invoked? ... registered under the Drivers\Active entry in registry. ...
    (microsoft.public.windowsce.platbuilder)