I am looking at generating C structures from XML data, generally speaking the C compiler must have a definition of something before it is used, which means an XML document tree would have to be written out starting from its leaves and going toward its root.

It doesn't seem like a split definition/declaration can be made of structs similar to functions.

Can it?

I don't know XML enough to know if nodes can have a circular or generally non linear relationship. Such as "rock > scissors" "scissors > paper", "paper > rock".

I wrote some testing:

struct _child
{
    char *child_name;

    struct _parent *father_reference;
};

struct _parent
{
    char *parent_name;

    struct _child *child_reference;
};

It seems declaring them out of order will work fine (all ISO standards compliant?) if the out of order members are pointers, but this can't be done with actual structures:

struct _child
{
    char *child_name;

    struct _parent father;
};

struct _parent
{
    char *parent_name;

    struct _child *child_reference;
};


main.c:6:17: error: field ‘father’ has incomplete type
  struct _parent father;
                 ^~~~~~

Not surprising exactly - how would you calculate the size of an object that grows by its on size at compilation time?

Could this be an issue when converting an XML defined data structure to a C structure implementation?

Would it be correct to say that there is a strict linear ordering to structure declarations in C?

1 Answers

0
Joshua On

Ah; you need forward declarations.

Add these declarations near the top*:

struct _child;
struct _parent;

Once given this much, the compiler can handle pointers to structures of these types. Your header declarations then compile.

*Once you get around to using header files, these go right after the double-include guard. Right now, put them right after standard includes.