> I do not see why you need to reserve space individually
> for the string, since we have reserved the heap memory for
> the node
Yes, and that node contains a
pointer to a string, not a string.
This you need a second malloc
Code:
struct dbase_rec {
struct dbase_rec *next;
char *record;
};
And this one, you don't need a second malloc
Code:
struct dbase_rec {
struct dbase_rec *next;
char record[MAX_STRING_LENGTH];
};
Try
Code:
printf( "Size=%d\n", sizeof( struct dbase_rec ) );
for both types of record structure.
The other important thing is that you should NOT be casting malloc in a C program.
> row = (dbase *) malloc (sizeof (dbase));
Should be written as
[tt]row = malloc (sizeof (dbase));[/tt]
Or preferably (since its easier to maintain)
[tt]row = malloc (sizeof *row );[/tt]
If you get a warning like
[tt] implicit declaration of function `malloc'[/tt]
It means that you failed to #include <stdlib.h>
This is a warning which you hide by casting, and it really is one which you should not be ignoring.
If you get a warning like
[tt] invalid conversion from `void*' to `char*'[/tt]
It means that you are using a C++ compiler to compile C code. If you really want to do this in C++, then you should be using new and delete (not malloc and free)
> char *rec = (char *) malloc (SZ_RECORD+1);
What is SZ_RECORD?
Surely the length of the string being copied would be most optimal.
char *rec = malloc(strlen(str)+1);
--