0

I'm currently programming in C, and had

typedef struct Stack Stack;

struct Stack {
    Stack above;
    Tree *t;
    char *tag;
};

I'm getting the error "field 'above' has incomplete type" I've already declared the struct in the typedef above it, so I'm not sure why I'm having issues. I've looked around, but haven't found anything that I can relate to this. Thanks!

Mystik
  • 49
  • 7
  • 1
    "I've already declared the struct in the typedef above it" But "typedef above it" declares it as an incomplete type, which is what the compiler is telling you. In any case, you can't embed a struct into itself. It makes no sense, if you think about it, since it produces an infinitely nested type. – AnT stands with Russia Feb 28 '18 at 01:27

1 Answers1

0

You can have a pointer to your struct inside your struct, but not an actual struct, which, for a stack, is probably what you are after.

typedef struct Stack Stack;

struct Stack {
    Stack *above;
    Tree *t;
    char *tag;
};

Just think of it, if you had struct B inside struct A, well then there would be a struct C inside struct B, and a struct D inside struct C...on and on.

struct Stack {
    Stack above {
        Stack above {
            Stack above {
                Stack above
                         ...............
                Tree *t;
                char *tag;
            };
            Tree *t;
            char *tag;
        };
        Tree *t;
        char *tag;
    };
    Tree *t;
    char *tag;
};
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31