|
|
Re: struct prototype
[Re: sheefo]
#111073
02/06/07 18:56
02/06/07 18:56
|
Joined: Sep 2003
Posts: 9,859
FBL
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 9,859
|
Note you're missing a ; in your 2nd struct definition. Using a void* pointer is working fine: struct_test.h: Code:
#ifndef STRUCT_TEST_H #define STRUCT_TEST_H
typedef struct { void* data; }STRUCT1;
typedef struct { STRUCT1* data; int value; }STRUCT2;
#endif
struct_test.c: Code:
/////////////////////////////////////////////////////////////// #include <acknex.h> #include <default.c>
#include "struct_test.h" ///////////////////////////////////////////////////////////////
STRING* value_str = "#12"; TEXT* debug_txt = { string (value_str); flags = VISIBLE; }
/////////////////////////////////////////////////////////////// function main() { STRUCT1 foo; STRUCT2 bar; bar.value = 42; foo.data = (void*)&bar; str_for_num(value_str, ((STRUCT2*)(foo.data))->value); while (1) { wait(1); }
}
This example also shows that using a void* Pointer will lead to some nasty typecasts later on. You should avoid this!
|
|
|
Re: struct prototype
[Re: jcl]
#111077
02/21/07 06:06
02/21/07 06:06
|
Joined: Nov 2005
Posts: 94 Texas
3Dski
Junior Member
|
Junior Member
Joined: Nov 2005
Posts: 94
Texas
|
For sure, the manual does say that the struct has to already be "defined", or itself, in order to be used inside a struct definition. You will be stuck with the use of "void *", which is rather nasty, or re-look at the problem you're trying to solve and come at it from a different angle. This is a interesting problem, so I'll keep looking at it to see if I see an general alternative.
AFTER THOUGHT:
Since A and B are obviously paired with one another in a prescribed way, why not just have a wrapper struct that contains the pointers to each? A and B would just contain their specific data. I see that this could potentially cause a problem if you had to look for a particular A or B with a series of C (container of A and B), but maybe you could place some kind of key in C that identifies the A-B pair (if you see you have to look them up):
typedef struct A{ ... data } A;
typedef struct B{ ... data } B;
typedef struct C{ A* a; B* b; } C;
... just a thought to play with.
Last edited by 3Dski; 02/21/07 06:20.
|
|
|
|