Linked List indirection errors
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
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.
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) );
Be sure to wrap this in a function
ghost_ptr = (BOB *) malloc(sizeof(BOB) );
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
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement