HeyMan
HeyMan

Reputation: 163

Recursive struct in C

#include <stdio.h>

typedef struct mystruct
{
    void (*ExitFnPtr)(mystruct);
    int a;
}mystruct;

int main()
{
    mystruct M;

    printf("Hello, World!\n");

   return 0;
}

Hi all, does anyone know how to solve the recursive struct error listed above?

Upvotes: 0

Views: 185

Answers (1)

mfro
mfro

Reputation: 3335

There is nothing recursive with that.

Your problem is just that the definition of mystruct is not known until the end of the struct's definition.

Try

typedef struct mystruct
{
    void (*ExitFnPtr)(struct mystruct ms);
    int a;
} mystruct;

struct mystruct is the same as mystruct (you just typedef it), but known at that point in time.

You could also do a forward declaration if you don't want to change your original code (although it's not as readable as the above:

typedef struct mystruct mystruct;

typedef struct mystruct
{
    void (*ExitFnPtr)(mystruct ms);
    int a;
} mystruct;

Upvotes: 4

Related Questions