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