1

I need the following construct:

// Header file

typedef fc_t (*comdevCbackIsReady_t)(const comdev_t* const module);

typedef struct
{
    comdevCbackIsReady_t    cbIsReady;  

} comdev_t;

This wont compile, because the comdevCbackIsReady_t function pointer type does not know the comdev_t struct type. If I exchange these 2 places, then the struct wont know the func pointer type. My current way around this is declaring the comdevCbackIsReady_t input parameter as void* instead. This is not very clean. Is there a way to somehow "forward declare" one of these?

EDIT this wont compile:

// Header file

struct comdev_t;

typedef fc_t (*comdevCbackIsReady_t)(const comdev_t* const module);

typedef struct
{
    comdevCbackIsReady_t    cbIsReady;  

} comdev_t;

The comdev_t is still an unknown type for the comdevCbackIsReady_t.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74

1 Answers1

2

In this typedef declaration of a function pointer

typedef fc_t (*comdevCbackIsReady_t)(const comdev_t* const module);

the used name comdev_t is not declared yet. So the compiler issues an error.

You could just introduce the tag name in the structure declaration like

struct comdev_t;

typedef fc_t (*comdevCbackIsReady_t)(const struct comdev_t* const module);

typedef struct comdev_t
{
    comdevCbackIsReady_t    cbIsReady;  

} comdev_t;

Or

typedef struct comdev_t comdev_t;

typedef fc_t (*comdevCbackIsReady_t)(const comdev_t* const module);

struct comdev_t
{
    comdevCbackIsReady_t    cbIsReady;  

};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Neither will work, either if initially compiles. As soon as any function is to be created to be passed as the created function pointer, it wont recognize the type. – Łukasz Przeniosło Jan 28 '23 at 21:59
  • @ŁukaszPrzeniosło The code I showed will work. If you have some other problems with a code that you did not show you should show it. Of course the structure declaration must be known in the translation unit where the functions are defined, – Vlad from Moscow Jan 28 '23 at 22:02
  • That is probably the problem. I would like to include that header and declare an actual function elsewhere- that's a different translation unit. – Łukasz Przeniosło Jan 28 '23 at 22:16
  • All and all, I think using a `void*` ptr is still the least hacky/ ugly solution, but thanks... – Łukasz Przeniosło Jan 28 '23 at 22:21