atv wrote:
Hi,
is there something wrong with this code?
Yes.
It seems that it crashes
at the else { clause, when allocating a second record.
If there were nothing wrong with the code, it wouldn't crash, would it?
Excuse for
the indentation, the g_warnings are from gtk.
Also, i'm using 2 pointers in the struct, which consists of 3 ints,
and 2 pointers, which i give 256 bytes each. Much to much ofcourse,
but it's just testcode.
The code doesn't even reach the g_warning in the { code.
Any help most appreciated.
atv
Well, the first thing to do is see what the compiler has to say...
if(tail==NULL) {
g_warning("First record in schedule allocated");
tail=(schedule *)malloc(256);
tail->string=malloc(256);
tail->timestamp=malloc(256);
strcpy(tail->string,string);
strcpy(tail->timestamp,asctime(tstruct));
tail->year=tstruct->tm_year;
tail->month=tstruct->tm_mon;
tail->day=tstruct->tm_mday;
tail->next=NULL;
head=tail;
} else {
g_warning("New record in schedule allocated");
/* First check if a day is already booked */
while(tmp!=NULL) {
g_warning("Checking for double appointment");
if(!strcmp(tmp->timestamp,asctime(tstruct))) {
gtk_widget_hide(window1);
return -1;}
tmp=tmp->next;
}
tail->next=(schedule *)malloc(256);
tail=tail->next;
tail->string=malloc(256);
tail->timestamp=malloc(256);
strcpy(tail->string,string);
strcpy(tail->timestamp,asctime(tstruct));
tail->year=tstruct->tm_year;
tail->month=tstruct->tm_mon;
tail->day=tstruct->tm_mday;
tail->next=NULL;
foo.c:1: parse error before `if'
foo.c:3: warning: type defaults to `int' in declaration of `tail'
foo.c:3: `schedule' undeclared here (not in a function)
foo.c:3: parse error before `)'
foo.c:6: parse error before `->'
foo.c:6: warning: type defaults to `int' in declaration of `strcpy'
foo.c:6: ANSI C forbids data definition with no type or storage class
foo.c:7: parse error before `->'
foo.c:12: warning: type defaults to `int' in declaration of `head'
foo.c:12: initializer element is not constant
foo.c:12: ANSI C forbids data definition with no type or storage class
foo.c:13: parse error before `}'
foo.c:21: warning: type defaults to `int' in declaration of `tmp'
foo.c:21: invalid type argument of `->'
foo.c:21: ANSI C forbids data definition with no type or storage class
foo.c:22: parse error before `}'
foo.c:24: warning: type defaults to `int' in declaration of `tail'
foo.c:24: redefinition of `tail'
foo.c:3: `tail' previously defined here
foo.c:24: invalid type argument of `->'
foo.c:24: ANSI C forbids data definition with no type or storage class
foo.c:25: parse error before `->'
foo.c:27: parse error before `->'
foo.c:27: warning: type defaults to `int' in declaration of `strcpy'
foo.c:27: ANSI C forbids data definition with no type or storage class
foo.c:28: parse error before `->'
make: *** [foo] Error 1
Perhaps getting it to compile relatively cleanly would be a sound first step
towards a solution.
--
Richard Heathfield :
bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ:
http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc:
http://users.powernet.co.uk/eton