tags:

views:

89

answers:

3

I'm looking for a good way to do something like this:

typedef struct user
{
  unsigned long id;
  //userList defined below
  userList friends;
}

typedef struct
{
  //userList contains users
  user * list;
  int count;
} userList;

Is there a legal way to do this or something similar?

+3  A: 

There is no way to reference a concrete (non-pointer) type before it's defined in C. You must use a pointer + a type declaration or actually define the type before it can be consumed.

JaredPar
+7  A: 

Do it like:

typedef struct user user;

typedef struct
{
  //userList contains users
  user * list;
  int count;
} userList;

struct user
{
  unsigned long id;
  //userList defined above
  userList friends;
};
nos
bleh I was too slow
Spudd86
Nice, but I like DigitalRoss' explanation of why it works
Richard Hoffman
+3  A: 

A struct definition can be an incomplete structure or union type by just defining or typedef'ing the struct tag. This can be used to declare pointers.

To declare an actual object, though, it can't be an incomplete type.

So, order your declarations so that the forward reference is the pointer and the backwards reference is the object.

DigitalRoss