Beginniner C Question: Two Structs Pointing to Each Other
So I have been learning C to build Max externals and recently came across a dilemna I can't quite solve, but am sure someone more familiar with the C language could help me out with.
Essentially, I need two structs pointing to each other. At first my problem was that one struct had to be declared before the other, and thus the struct that got declared second wasn't a defined data type when it was declared within the first struct. I tried to overcome this by making the struct within the first struct a void pointer rather than the second struct's datatype. However, in one of my functions, I need to pass the second struct's datatype; trying to pass the void pointer seems to be causing Max to crash.
Here's a breakdown of the problem:
struct1 = onebang
struct2 = genbang
there are 16 onebangs contained as an array within genbang
i want each onebang to have a reference to the genbang that its contained within
but one struct has to be declared before the other, so one data type will be undefined upon declaration
tried creating void *genbang within each onebang, then in the new function, setting each onebang's void*genbang = genbang. like this:
for(i=0; i
genbang->onebang[i].genbang = genbang;
However, I have a function that needs to have a t_genbang datatype passed to it. The void * data type within the onebang struct causes a crash. In the code, this function is the "incremement" function, and its called by the clock_function.
Attached is the code. Thanks for your time.
Try casting the void* to t_genbang* like:
increment((t_genbang*)ob->gb, ob->number);
Davis
that was a good suggestion, but i seem to be getting the same crash.
Incomplete types are your friend. So you declare prototypes for both structs before you define them, something like:
typedef struct _structA t_structA;
typedef struct _structB t_structB;
struct _structA {
t_structB b;
}
struct _structB {
t_structA a;
}
ah, i am familiar with function prototypes and expected some kind of equivalent for structs. thanks.