#12389 - frag - Mon Nov 10, 2003 2:56 pm
Okay I'm going slightly insane here.
Code: |
typedef struct linkedList {
linkedList *Next;
} linkedList; |
Pretty standard linked list code. I'm using GCC and it spits out the following:
Code: |
state.h:8: parse error before "linkedList"
state.h:8: warning: no semicolon at end of struct or union
state.h:9: warning: type defaults to `int' in declaration of `linkedList'
state.h:9: warning: data definition has no type or storage class |
Thinking maybe it was some weirdness with both the type and struct being the same name I tried the following:
Code: |
typedef struct linkedListStruct {
linkedListStruct *Next;
} linkedList; |
And this gave me the same errors as before.
Maybe its just late or something, but this is driving me crazy. I can't figure out what I'm doing wrong.
Any help would be great.
Frag
ADDENDUM:
If I try to compile without the self-referential bit its fine. but that sort of defeats the purpose of the linked list to begin with ;-)
#12390 - tepples - Mon Nov 10, 2003 4:25 pm
frag wrote: |
Okay I'm going slightly insane here.
Code: | typedef struct linkedList {
linkedList *Next;
} linkedList; |
|
If you're making a self-referential struct (list, tree, etc) in C, try using the struct tag instead of the typename within the struct definition:
Code: |
typedef struct linkedList {
struct linkedList *Next;
} linkedList; |
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#12391 - frag - Mon Nov 10, 2003 4:50 pm
Christ it really is too late to be coding. heh.
Thanks tepples. It would have bit me in the nose if it had teeth.
#12395 - dagamer34 - Tue Nov 11, 2003 12:54 am
You cannot typedef something and call it the same thing!!!!! (This is like a basic C error, so excuse me for my rudeness)
There i am calm now.
Here is what you should do:
typedef struct LinkedList {
linkedList *Next;
} linkedList;
Notice the capitalization of the first letter. Your problems should disappear.
Your welcome.
_________________
Little kids and Playstation 2's don't mix. :(
#12397 - tepples - Tue Nov 11, 2003 1:30 am
dagamer34 wrote: |
You cannot typedef something and call it the same thing!!!!! |
Really? What part of the C language specification prevents a module from having a typename and a struct with the same name? For example, do you claim that the following, which I claim is valid C, would be invalid?
Code: |
struct LIST {
int car;
struct LIST *cdr;
};
typedef struct LIST LIST;
|
The following should be equivalent:
Code: |
typedef struct LIST {
int car;
struct LIST *cdr;
} LIST;
|
Just make sure that when recursively defining structs, define them in terms of their struct names rather than their typenames.
It is only in C++ that struct names and typenames share a namespace, and even there, C++ has an exception that allows for this idiom.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#12606 - sgeos - Wed Nov 19, 2003 8:54 am
The compiler needs to know what a type is before it can deal with it. It can deal with struct tags even before you declare the struct. It can not deal with anything typedef'ed until the type has been defined, that is after the semicolon. This code compiles:
Code: |
#include <stdio.h>
struct a {
int data;
struct b *b;
};
struct b {
char *data;
struct a *a;
};
int main(void)
{
struct a a = {1234, 0};
struct b b = {"asdf", &a};
printf("%s%d\n", b.data, b.a->data);
return 0;
} |
struct a contains a pointer to type struct b. struct b is defined later. If you wanted to typedef all of that, you could do something like this:
Code: |
typedef struct a {
int data;
struct b *b;
} a;
typedef struct b {
char *data;
a *a;
} b; |
Although I'd advise using the struct tags (look at the definition of struct b). I used to typedef all of my structs, but years ago somebody pointed out that that can make one's code more difficult to read. If we see something like this:
We have no idea what fp is. It could either of these, or something else altogether:
Code: |
tpyedef unsigned long fp; /* Fixed point */
typedef struct fp {
FILE *f; /* File */
int c; /* Made changes since last save? */
} fp; |
I'm done being "helpful". (Read: I'm done ranting.)
-Brendan
#12621 - Touchstone - Wed Nov 19, 2003 6:40 pm
tepples wrote: |
dagamer34 wrote: | You cannot typedef something and call it the same thing!!!!! |
Really? What part of the C language specification prevents a module from having a typename and a struct with the same name? |
But tepples, DaGamer34 is using five (5!) exclamation-marks, he must be knowing what he's talking about.
Zap!
_________________
You can't beat our meat
#12631 - sgeos - Thu Nov 20, 2003 12:17 am
Touchstone wrote: |
tepples wrote: | dagamer34 wrote: | You cannot typedef something and call it the same thing!!!!! |
Really? What part of the C language specification prevents a module from having a typename and a struct with the same name? |
But tepples, DaGamer34 is using five (5!) exclamation-marks, he must be knowing what he's talking about.
Zap! |
meaning = !!!!!("You cannot typedef something and call it the same thing");
-Brendan
#12640 - poslundc - Thu Nov 20, 2003 3:03 am
Now that's bad. I don't know if I'm more ashamed of you for making the joke or myself for getting it.
Dan.
#12641 - sgeos - Thu Nov 20, 2003 3:37 am
Glad I could be of service. (Or !, whatever the case may be.)
-Brendan