Quick Question in C ... simple structs won't compile

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
typedef struct student_s {
char *name;
int age;
char *school;
int year;
} Student;

typedef struct node_s {
Student person;
Node *next;
} Node;

The errors I get are

Error : ';' expected
linkedList.c line 12 Node *next;

Error : declaration syntax error
linkedList.c line 13 } Node;

I can't see anything wrong... can you?
 

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
I don't believe you need the name 'Student' or 'Node' beneath the structure but you do need the semicolons after the structure. I always thought it was char* and not char *. I dunno but is probably a pointer problem.
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
yes you do.... it's a typedef... i'm typedefing the first struct to the name student... the pointers are ok too... anyway... can someone please address the error in the second struct?
 

newbiepcuser

Diamond Member
Jan 1, 2001
4,474
0
0
typedef struct student_s {
char *name;
int age;
char *school;
int year;
} Student;

typedef struct node_s {
Student person;
node_s *next;
}Node;

Ai yo, try this

using MS VS 6.0 sp4

 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
still the same error.... tried to compile with gcc this time instead of codewarrior.... here's what i get

main.c:12: parse error before `Node'
main.c:12: warning: no semicolon at end of struct or union
main.c:13: warning: data definition has no type or storage class

if that helps...
 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
Originally posted by: cchen
still the same error.... tried to compile with gcc this time instead of codewarrior.... here's what i get

main.c:12: parse error before `Node'
main.c:12: warning: no semicolon at end of struct or union
main.c:13: warning: data definition has no type or storage class

if that helps...

try changing it from Student to struct Student and drop the typedef in the declaration to see if that changes the errors.

-silver
 

BCYL

Diamond Member
Jun 7, 2000
7,803
0
71
Do this:

typedef struct node_s {
struct student_s person;
struct node_s *next;
} Node;

 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
ahhh
i just fixed it
i changed Node to struct node_s
i dunno why that worked and it didn't work originally... since Node is technically the same as node_s


thanks guys for all your assistance
 

BCYL

Diamond Member
Jun 7, 2000
7,803
0
71
Originally posted by: cchen
ahhh
i just fixed it
i changed Node to struct node_s
i dunno why that worked and it didn't work originally... since Node is technically the same as node_s


thanks guys for all your assistance

It didnt work because at the point you declared "Node *next;", the typedef instruction hasnt completed yet... so Node has not been mapped to your structure yet... That's why you have to use struct node_s....

 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
Originally posted by: BCYL
Originally posted by: cchen
ahhh
i just fixed it
i changed Node to struct node_s
i dunno why that worked and it didn't work originally... since Node is technically the same as node_s


thanks guys for all your assistance

It didnt work because at the point you declared "Node *next;", the typedef instruction hasnt completed yet... so Node has not been mapped to your structure yet... That's why you have to use struct node_s....

Yup, yup!

-silver