Recursive struct in C

前端 未结 1 1962
渐次进展
渐次进展 2021-01-28 05:10
#include 

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

int main()
{
    mystruct M;

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


        
相关标签:
1条回答
  • 2021-01-28 05:32

    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;
    
    0 讨论(0)
提交回复
热议问题