cryptic gcc warning: function ponter typedef w/ struct pointer argument

Started by
1 comment, last by DracoLacertae 10 years, 2 months ago

I have a struct with a function pointer in it. This compiles fine:


typedef struct gx_quadpatch_s
{
    gx_vbuffer_t* vb;
    struct gx_quadpatch_s* children[4];
    struct gx_quadpatch_s* parent;

    //...

    void (*detailer)(struct gx_quadpatch_s* dest, struct gx_quadpatch_s* source, int a_start, int a_end, int b_start, int b_end);



} gx_quadpatch_t ;

I wanted a pretty typedef for the function type:


typedef     void (*gx_quadpatch_detailer_f)(     struct gx_quadpatch_s* dest, struct gx_quadpatch_s* source, int a_start, int a_end, int b_start, int b_end);


typedef struct gx_quadpatch_s
{
    gx_vbuffer_t* vb;
    struct gx_quadpatch_s* children[4];
    struct gx_quadpatch_s* parent;

    //...

    gx_quadpatch_detailer_f detailer;

} gx_quadpatch_t ;

GCC gives this warning:


In file included from gx_quadpatch.c:18:0:
../graphics/gx_quadpatch.h:5:80: warning: 'struct gx_quadpatch_s' declared inside parameter list [enabled by default]
 typedef  void (*gx_quadpatch_detailer_f)(  struct gx_quadpatch_s* dest, struct gx_quadpatch_s* source, int a_start, int a_end, int b_start, int b_end);
                                                                                ^
../graphics/gx_quadpatch.h:5:80: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]

This suggests I need to declare the struct quadpatch_s definition before the gx_quadpatch_detailer_f. Circular dependency. Not sure how to break it.

Usually if I have an interdependency between structs, I can do this:


typedef struct foo_s
{
    struct bar_s* my_bar;

} foo_t;

typedef struct bar_s
{
   struct foo_s* my_foo;
} bar_t;

After that, I can refer them at foo_t and bar_t. In its allowed for a struct to defined as containing a pointer to another struct that hasn't yet because all struct pointers are the same size. Can I not do the same thing with function argument lists?

Is there something I can put above the gx_quadpatch_detailer_f typedef to resolve this?

Advertisement

Just add


struct gx_quadpatch_s;

above the function typedef to forward declare the struct.

Wow thanks. I didn't know I could forward reference a struct like that. upvote for you!

This topic is closed to new replies.

Advertisement