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 3342656
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:51:52+00:00 2026-05-18T00:51:52+00:00

This is unfortunately defined in some external library: cannot touch! // library.h typedef struct

  • 0

This is unfortunately defined in some external library: cannot touch!

// library.h
typedef struct {
    long foo;
    char *bar;
   /* ... (long & complex stuff omitted) */
} *pointer_to_complex_struct_t;

Now The Question: how to declare an complex_struct_t variable?

  • Ideal solution but not allowed! (cannot change external library):

    // library.h
      /* ... (long & complex stuff omitted) */
    } complex_struct_t, *pointer_to_complex_struct_t;
    
    
    // my.h
    extern complex_struct_t my_variable;
    
  • Non-portable solution (gcc):

    // my.h
    extern typeof( * (type_placeholder)0 )  my_variable; // Thanks caf!
    
  • Other? Better? Thanks!

Bonus question: same question for a function pointer (in case there is any difference; I doubt it).


ADDED bonus: below is the exact same question but with functions instead of structs. This should not make any difference to the short answer (“No.”), the only answer I was initially interested in. I did not expect some people to die trying to know and get my job done with creative workarounds, which is why I simplified the question from functions to structs (function pointers have special implicit conversion rules for convenience and confusion). But hey, why not? Let’s get the copy-paste workaround competition started. Some workarounds are probably better than others.

///// library.h //////
// Signature has been simplified
typedef double (*ptr_to_callback_t)(long, int, char *);
// Too bad this is not provided: typedef double callback_t(long, int, char *);

///// my.h /////
// This avoids copy-paste but is not portable
typedef typeof( * (ptr_to_callback_t)0 ) callback_t;

extern callback_t callback_1;
extern callback_t callback_2;
extern callback_t callback_3;
// etc.

Short answer = no, there is currently no portable alternative to typeof

A basic copy-paste workaround works OK for functions but not for structs. The compiler will match the duplicated function types, but will not relate the duplicated struct types: a cast is required and the duplicated struct types will diverge without compilation warning.

  • 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-18T00:51:52+00:00Added an answer on May 18, 2026 at 12:51 am

    No, unfortunately you cannot do it with standard C. With C++ a simple metafunction would do the trick though.

    However you could just copy-paste the definition of the struct thus leaving the original untouched

    typedef struct {
     ///same  struct
    } complex_struct_t;
    

    The downside of this solution is that the expression &complex_struct_t won’t be of type pointer_to_complex_struct_t, instead it will be of type pointer to unnamed struct {//your members};
    You’ll need reinterpret_casting, if you need that feature…

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

Sidebar

Related Questions

Unfortunately the BOL is a little vague on this, but index_id = 0 on
Unfortunately, I need to do this. I'm using ELMAH for my error log. Before
I've have searched on this and it seems to be a catch all, unfortunately
I am aware of this command: cvs log -N -w<userid> -d1 day ago Unfortunately
Is there a good equivalent implementation of strptime() available for Windows? Unfortunately, this POSIX
This is a bit of a long shot, but if anyone can figure it
Currently I'm using a base class for some Customer model classes. As defined as
C# unfortunately does not allow for extra user-defined syntax. But I was wondering whether
I'm writing a C++ application that makes heavy use of OpenCV. Unfortunately, some of
I know this would be easy with position:fixed, but unfortanately I'm stuck with supporting

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.