2

I have a function of type void(*func)(int) and I get a pointer to this function, how can I know for sure that a pointer is really of this type?

if (typeof(pointer) == void(*func)(int)) {
    //do something     
} 

Is that correct?

edit - I get the pointer from a function as an argument and I want to check the type of the pointer inside this function.

Jens
  • 5,767
  • 5
  • 54
  • 69
Dean Taler
  • 737
  • 1
  • 10
  • 25
  • If you have a `void (*pointer)(int)`, then its type *is* `void (*)(int)`. There's nothing to check. Why do you think it would be any different? – Kevin May 08 '20 at 15:59
  • C is a statically typed language. By the time it gets into the variable it is a pointer of the specified type. – Raymond Chen May 08 '20 at 16:05
  • 1
    Does this answer your question? [How do I check if a variable is of a certain type (compare two types) in C?](https://stackoverflow.com/questions/6280055/how-do-i-check-if-a-variable-is-of-a-certain-type-compare-two-types-in-c) – RobertS supports Monica Cellio May 08 '20 at 16:06
  • @DeanTaler: you can accept one of the answers by clicking on the grey checkmark below its score – chqrlie May 11 '20 at 19:56

3 Answers3

4

if you are getting your function pointer dynamically , so try to use union to and a flag variable to get it's type without checking something complex

#define type1 1
#define type2 2
union FN
{
void ....
int ....
....
}

typedef struct{
int type;

union FN fn;
} Function;
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
  • 1
    Interesting. Could you add some sample code to your answer to make it more complete and not just a single sentence answer? – pastaleg May 08 '20 at 16:07
  • I´m not quite sure what you mean but note that the OP wants to have the check at runtime, not compile-time. – RobertS supports Monica Cellio May 08 '20 at 16:10
  • check it after editing ,it's useful for more dynamically ,but to be honest i don't know where he wants to get the pointer function from –  May 08 '20 at 16:15
  • but I want to chek the pointer inside a fucntion that gets the pointer, so it looks more complicate to do it in this way, is there no easier option? – Dean Taler May 08 '20 at 16:17
  • `void(*func)(int)` is like checking if `type==TYPE1` –  May 08 '20 at 16:20
3

typeof not a part of the C standard. It is a GNU extension.

If you want to be sure that something is of a particular type, you can handle it at compile-time with _Generic introduced in C11:

_Generic((pointer), 
    void(*)(int): /* insert your code here */,
)

If said pointer is not of type void(*)(int), then it will fail to compile.

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
3

If your function gets the pointer as an argument and has this prototype:

void my_function(void (*func)(int)) {
    // How can I test if `func` really points to a function taking an `int`?
}

Then there is not much you can do inside the body of my_function to verify what func really points to.

  • you can test if it is a null pointer:

    if (!func) { /* NULL was passed */ }
    
  • you can test if it points to a known function:

    if (func == exit) { /* haha! (*func)(1) will abort */ }
    
  • beyond the above, it is impossible to tell what was actually passed to my_function: it could be an actual function with the correct prototype, or another function implicitly or explicitly cast to the expected type or even some other scalar object with a cast.

If you want to write a macro whose expansion depends on the type of an expression, you can use the new C11 _Generic construction:

#define IS_VOID_FUNC_OF_INT(func)  _Generic(func, void(*)(int): 1, default: 0)

But using this inside the body of function my_function above will always evaluate to 1: only the defined type of func is tested by _Generic, not that of the original expression used as an argument to the function.

chqrlie
  • 131,814
  • 10
  • 121
  • 189