Linked List indirection errors

Started by
3 comments, last by Fruny 19 years ago
Hello, I'm having problems making a linked list in C. My structure looks like typedef struct BOB_TYP { data..... struct BOB_TYP *next; }BOB, *BOB_PTR; I declare a pointer to it externally in a .h so it is global extern BOB *ghost_ptr; I then declare it in declarations.cpp so all cpps can see it BOB *ghost_ptr; ghost_ptr = (BOB *) malloc(sizeof(BOB) ); When I build the code I get these errors: error C2501: 'ghost_ptr' : missing storage-class or type specifiers error C2040: 'ghost_ptr' : 'int' differs in levels of indirection from 'struct BOB_TYP *' error C2440: 'initializing' : cannot convert from 'struct BOB_TYP *' to 'int' Are there any glaring errors in my code that i'm missing. Any help would be greatly appriciated. Thanks, Josh
Advertisement
Forgive me for asking, isn't there in C, or, C#, or C++, a 'TList' or a 'TObjectList' Feature (like in Delphi).

in that way you don't need any linked lists anymore! They are pain!

Linked List, if you do not really understand the way they work, they can be a real pain. A linked List is just a bunch a Structs (In pascal called Record) which are dynamically placed on the heap.

In order to solve your problem I'd suggest to study, deeply, the nature of Linked Lists.

Best Regards,
Marmin.
It seems to me that all declarations aren't 'seeing' BOB. Put the typedef for BOB into a.h and include a.h in any cpp file that needs to declare a variable of type BOB.


Be sure to wrap this in a function

ghost_ptr = (BOB *) malloc(sizeof(BOB) );

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
This doesn't make much sense to me, but if I move the line:

ghost_ptr = (BOB *) malloc(sizeof(BOB) );

into the main part of my prog, I no longer get the errors. Not really sure why I can't call the memory right after I declare it. I'm kinda new to using multiple cpps.

Thanks for your replies... Josh
That's because you can't put arbitrary code at global scope. Try BOB *ghost_ptr = (BOB *) malloc(sizeof(BOB) );, so that it is an initialization.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement