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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:34:26+00:00 2026-06-12T10:34:26+00:00

How do I declare a function pointer that can safely point to any function?

  • 0

How do I declare a function pointer that can safely point to any function?

I need something like void pointers, but for functions. I thought about simply using that but, according to the answers to this question, function pointers cannot be reliably converted to data pointers, at least on non-POSIX systems.

I searched and found some possible examples of this:

  • Windows has FARPROC:

    int (FAR WINAPI * FARPROC) ()
    

    The documentation also mentions this:

    In C, the FARPROC declaration indicates a callback function
    that has an unspecified parameter list.

    Sounds fine, but what about the return value? It is clearly specified as int.

  • One of OpenGL’s specifications has:

    typedef void (*GLfunction)();
    extern GLfunction glXGetProcAddressARB(const GLubyte *procName);
    

    The parameter list is also unspecified, but the return type is clearly specified as void.

I’m not sure how to interpret this. The function pointer will be cast to a function pointer type with the appropriate prototype before it is used. For example:

typedef void (*function_pointer_t)();
extern function_pointer_t get_function(const char * name);

/* Somewhere else... */

typedef double (*cosine_function_t)(double);
cosine_function_t cosine = (cosine_function_t) get_function("cos");

Is this code safe? Does it violate any C standard or result in any undefined behavior?

  • 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-06-12T10:34:27+00:00Added an answer on June 12, 2026 at 10:34 am

    Firstly, there’s no way to declare a function pointer that would be able to accept any function pointer type without a cast. The problem, as you already noted yourself, is that any function pointer declaration immediately assumes a specific return type. So, there’s no such thing as full analogue of void * in function pointer world.

    Secondly, any function pointer can be used to store any other function pointer value as long as you force the conversion by using an explicit cast. Of course, in order to perform a proper call through a pointer forcefully converted to a different type you need to convert it back to the original type. I.e. this does not cause any undefined behavior as long as the proper function pointer type is restored at the moment of the call.

    In your example if the pointer returned by get_function really points to a function of double (double) type, then it is perfectly safe to call that function through cosine pointer. The fact that the pointer value was stored intermediately in a void (*)() pointer does not destroy it.

    Thirdly, in C language a () parameter declaration stands for unspecified number and types of parameters. This is a step in the direction of the "universal" function pointer type that can be used without a cast (as long as you supply proper parameters in the call and as long as the return type matches)

    void foo(void);
    void bar(int i);
    void baz(long i, double x);
    
    int main() {
      void (*a[3])() = { foo, bar, baz };
      a[0]();
      a[1](42);
      a[2](5L, 3.1415);
    }
    

    But, again, the problem of return type still remains.

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

Sidebar

Related Questions

Is it possible to declare a function pointer (non C++ 11) that can point
With respect to this question, we can declare a function that returns pointer to
I can forward declare a function in a namespace by doing this: void myNamespace::doThing();
I have C function that takes string pointer as a parameter. This functions returns
I'm trying to do something with pointers to function members that I cant' figure
I have a function that I need to call int (*trytoprint_f)(int val, void *param);
can we declare a function in a header file in following way? extern int
Say I declare a lua function using the : operator like so: function ClassName:myFunc(
In the following example, I tried to use uninterpreted Boolean function like (declare-const p
I have next class and try to declare member function which will return pointer

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.