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.

2
  • You can't compile header files. Commented Jan 28, 2023 at 22:08
  • you need to look at the answer below to get the correct syntax Commented Jan 28, 2023 at 23:05

1 Answer 1

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;  

};
Sign up to request clarification or add additional context in comments.

4 Comments

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.
@Ł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,
That is probably the problem. I would like to include that header and declare an actual function elsewhere- that's a different translation unit.
All and all, I think using a void* ptr is still the least hacky/ ugly solution, but thanks...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.