2

For example,

typedef struct { char * name; component ** components; } object;

typedef struct { vector3 pos, orient; object * obj; } component;

I've already tried as is and, predictably, I get an error that the first one contains a pointer to a pointer of a "component", which the compiler doesn't recognise yet. Is there a way around this?

1 Answers1

1

You need a forward declaration of the second struct in order to satisfy the pointer declaration in the first:

struct component; // This says there is a struct named component somewhere in the code

typedef struct { char * name; struct component ** components; } object;

struct component { vector3 pos, orient; object * obj; };
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 2
    A tagged struct forward declaring an untagged one? – StoryTeller - Unslander Monica Jun 28 '20 at 06:12
  • @StoryTeller-UnslanderMonica It's been a while since I've done pure C. I'm figuring out the details to make sure my answer is correct. I probably should do that before posting a finished answer. – Code-Apprentice Jun 28 '20 at 06:13
  • 1
    I think there is no `struct component` type defined. Only a `component` type (the struct is anonymous). – kaylum Jun 28 '20 at 06:13
  • I'm going to give it a go, will report back in a minute –  Jun 28 '20 at 06:14
  • @kaylum Feel free to post an answer or give constructive comments about how to actually fix mine. – Code-Apprentice Jun 28 '20 at 06:14
  • I've added a forward declaration, it seems to be throwing the same error still –  Jun 28 '20 at 06:15
  • I thought that was a pretty constructive comment as I assumed you would be able to edit the answer once pointed out to you. – kaylum Jun 28 '20 at 06:15
  • @sfajaja As others are ruthlessly pointing out, my exact syntax isn't correct. But the concept is what you need. Googling "forward declaration struct C" or something like that should fill in the details you need. – Code-Apprentice Jun 28 '20 at 06:15
  • @kaylum As I said in the previous comment to Story Teller, I haven't done C in a while and don't know the exact syntax off the top of my head for a forward declaration, so no, I'm not able to just edit it like you assumed. – Code-Apprentice Jun 28 '20 at 06:16
  • Does this mean I need to abandon my typedefs for any such structures? edit: stupid question, nevermind –  Jun 28 '20 at 06:18
  • @sfajaja Maintaining typedefs is the part I haven't figured out yet, if it's even possible. – Code-Apprentice Jun 28 '20 at 06:45
  • Just give the struct a name, e.g. `typedef struct component { vector3 pos, orient; object * obj; } component;` (the typedef and struct tag don't conflict, they are in separate namespaces). Now your forward declaration of `struct component;` will work and then for object `typedef struct { char * name; struct component **components; } object;` – David C. Rankin Jun 28 '20 at 07:13
  • Anyway this is a duplicate, better to contribute to that instead. – Antti Haapala -- Слава Україні Jun 28 '20 at 09:45
  • @Code-Apprentice to answer your question, the answer is yes. In my source files I just use all raw struct syntax followed by typedefs for use in other files, with forward declarations in a header file. Basically `struct a; struct b; struct a { struct b * ptr; }; struct b { struct a * ptr; }; typedef struct a a; typedef struct b b;` Works fine. –  Jun 28 '20 at 11:54