Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

mutual referential structure pointers

keyekeye Member Posts: 2
I am testing mutual referential structure pointers( which is used for thread lib later). the code can't compiled, keep reminding me add 'qualifer-list' before B_t


#include
#include

typedef struct A{

int id;
B_t b;
}*A_t;

typdef struct B{

int id;
A_t a;
}*B_t;


int main()
{
return 0;
}

I tried to add 'B_t' just after header files but doesn't work.
Any idea is well apprecaited! Thanks you guys.


Kunsheng

Comments

  • LundinLundin Member Posts: 3,711
    I always wonder why people need these kind of data types...
    This is what I would have done:

    [code]typedef struct A{

    int id;
    A a;
    Specifics specifics;

    } A_t;
    [/code]

    If A and B are different, store the differencies in the "specifics" variable.
  • Malcolm_McLeanMalcolm_McLean Member Posts: 254 ✭✭

    : #include
    : #include
    :
    : typedef struct A{
    :
    : int id;
    : B_t b; /* struct B *b; */
    : }*A_t;
    :
    : typdef struct B{
    :
    : int id;
    : A_t a; /* struct A * a */
    : }*B_t;
    :
    :
    If an A contains a B then a B cannot, tardis-fashion, contain an A. That's a basic feature of data structures.
    However A and B can contain pointers to each other. One of the quirks of C is that the compiler won't look ahead to the typedef. So you need to put the struct keyword in the definition of A, and it's neater though not required to hav it in B as well.
  • LundinLundin Member Posts: 3,711
    Yeah they need to be pointers. I still can't see when you would need such a structure. If you translate this to object orientation, then A and B could be two classes inherited from a base class. If the programmer then detects that A needs to access B and vice versa, and decides to do it through pointers, something is fundamentally wrong in his program design. If they have anything in common, it should be placed in the base class.
  • keyekeye Member Posts: 2
    Thanks you guys. I am sorry I didn't mention clearly what was happening.

    My situation is awful. I have a structure thread and a structure thread_queue. thread_queue, of course, contains a pointer to a structure thread.

    At the same time, a structure thread contains a pointer to a thread_queue (which record the threads waiting for its returnvalue). Also I am forced to use 'typdef struct A{ ... }*A_t' here.


    My code is complete as below. Both 'A_t' and 'B_t' are structure pointers.
    it keep reminds me
    'structure.c:9: error: expected specifier-qualifier-list before
Sign In or Register to comment.