Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4039620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:38:20+00:00 2026-05-20T12:38:20+00:00

I need to declare a (typedef’d) structure and a (typedef’d) function reference in pain

  • 0

I need to declare a (typedef’d) structure and a (typedef’d) function reference in pain old C. This is my code:

typedef void (*monitor_calback)(monitor_data*, short int, short int, void*);

typedef struct
{
    int port;
    unsigned char port_state;

    monitor_calback cb_high[8];
    void *cb_high_data[8];
    monitor_calback cb_low[8];
    void *cb_low_data[8];
} monitor_data;

But of course it doen’t compile because we don’t know about the structure when the function reference is declared.

I have gotten this but it looks kinda messy and is a little hard to read.

struct _monitor_data;

typedef void (*monitor_calback)(struct _monitor_data*, short int, short int, void*);

typedef struct _monitor_data
{
    int port;
    unsigned char port_state;

    monitor_calback cb_high[8];
    void *cb_high_data[8];
    monitor_calback cb_low[8];
    void *cb_low_data[8];
} monitor_data;

Are there any better ways to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:38:20+00:00Added an answer on May 20, 2026 at 12:38 pm

    You can typedef a struct before defining it:

    typedef struct _monitor_data monitor_data;
    
    typedef void (*monitor_calback)(monitor_data*, short int, short int, void*);
    
    struct _monitor_data
    {
        int port;
        unsigned char port_state;
    
        monitor_calback cb_high[8];
        void *cb_high_data[8];
        monitor_calback cb_low[8];
        void *cb_low_data[8];
    };
    

    This will work fine as long as you don’t try to reference the internal structure of monitor_data before struct _monitor_data is fully defined. All the compiler needs to know for your monitor_callback definition is that monitor_data * is a pointer to something so monitor_callback is fine as long as the compiler knows that monitor_data exists.

    This sort of construct is the standard approach for defining opaque types in C, you’d just be un-opaquing your type rather than leaving it opaque.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to declare an array of pointers to functions like so: extern void
I think in this case there is no need to declare a public constructor
I have an enum in my objective-C code similar to this: typedef enum {
I want to write reusable code and need to declare some variables at the
I need to declare the query variable outside the switch statement that way I
Need a function that takes a character as a parameter and returns true if
typedef struct _stats_pointer_t { char **fileNames; stats_t stats; } stats_pointer_t; I need to fill
I'm doing some policy-based designs here and I have the need to typedef lots
Do I really need three statements, i.e. like this class A; template<class _T> class
This is for the C preprocessor experts: How can I declare an enum with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.