Forward define struct AND type without any warnings

283 views Asked by At

Our universitie's homework submission system has - instead of focusing on programming skills - set ridiculous and impractical requirements on submissions. To bypass that, I use preprocesor and few other tricks to merge my homework solution into one file (one of the requirements).

Another requirements is that no warnings must occur - and -Wpedantic is enabled. I forward-declare a struct in node.h so that I can use it in function calls:

typedef struct Edge Edge;
// So that I can do:
typedef struct Node {
    void* value;
    int name;
    Array* edges;
} Node;
Edge* node_find_edge(Node* node, NodeName target);

In a different file - edge.h - full definition reads as:

typedef struct Edge {
    size_t cost;
    NodeName A;
    NodeName B;
} Edge;

I get this warning:

main.c: At top level:
main.c:779:3: warning: redefinition of typedef 'Edge' [-Wpedantic]
 } Edge;
   ^
main.c:753:21: note: previous declaration of 'Edge' was here
 typedef struct Edge Edge;

Don't get confused by the "main.c" thing, that simple because the files are merged as I said.

What forward declaration is correct and warningless?

1

There are 1 answers

10
Barmar On BEST ANSWER

Don't use the typedef, just use struct Edge*

struct Edge; // forward declaration
struct Edge* node_find_edge(Node* node, NodeName target);