4

I know this is a duplicate. I've searched already but none address the problem I'm having. NOTE in an attempt to single out my confusion I have tried to simplify the typecast from the original. Hopefully it isn't undefined behaviour

Declaring a pointer to a function

Int (*funcPtr)(void*,void*);

Strcmp declaration

int strcmp (char *, char *);

Typecasting a function pointer

FuncPtr = (int (*)(void*, void*))strcmp

This is where I get confused. I understand Typecasting a function pointer we have created. So type casting FuncPtr for example just changes what type of function funcPtr can point to. I have also read that the name of a function Is a function pointer to itself so Typecasting strcmp and using my understanding from above,

int (*)(void*, void*))strcmp 

Strcmp can now point to a function that takes two pointers to void and returns an int which Is clearly wrong as like arrays I think the function name can't be a pointer to another function.

Because of my understanding above I fail to see how you can assign the address of strcmp to FuncPtr with the following typecast

FuncPtr = (int (*)(void*, void*))strcmp

As changing what the function pointer strcmp points to doesn't change the arguments and return type of strcmp.

I would really appreciate some knowledge I'm just so confused.

11
  • 3
    Well, just because you can, doesn't mean it's a good idea. I can drive my car into a tree if I want to, but.... Commented May 8, 2017 at 7:54
  • 1
    Any function pointer can point to any function but you are not allowed to call the function through the pointer unless the pointed-to type exactly matches the function being pointed to Commented May 8, 2017 at 7:54
  • "I think the function name can't be a pointer to another function" Eh? What do you even mean here? And what is FuncPtr, what type? Please note that the C language is case-sensitive. Commented May 8, 2017 at 8:01
  • If the question is simply "can I have a function pointer of type int (*)(void*, void*)) and then cast the address of strcmp to that type?" then I can answer it. All the other things in your question makes it unclear, however. Also for the record, strcmp has the following format, which matters here: int strcmp(const char *s1, const char *s2);. Commented May 8, 2017 at 8:04
  • What I mean is by typecasting strcmp changes what strcmp points to. However I don't think it's possible to change what strcmp points to Commented May 8, 2017 at 8:08

1 Answer 1

10

There are functions and there are function pointers. strcmp is a function, linked to your project. When the identifier strcmp is used in an expression, you get a function pointer to that function. But you cannot change "where strcmp points to" because strcmp is a function and not a pointer.

C allows conversions between different function pointer types. However, should you try to call a function through a function pointer of incompatible type, you invoke undefined behavior.

int (*funcPtr)(void*,void*);

is not compatible with strcmp, which has the following format:

int strcmp(const char *s1, const char *s2);

Meaning:

funcPtr = (int(*)(void*, void*))strcmp; // this is ok but not very meaningful
funcPtr(); // this is NOT ok, invokes undefined behavior

In order to actually call the function strcmp through that function pointer, you would have to cast back to the correct type, which is int (*)(const char*, const char*).

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

3 Comments

"A function name (label) is converted to a pointer to itself." denniskubes.com/2013/03/22/basics-of-function-pointers-in-c the last chapter discusses this
I'm still cunfused as to what the cast (int()(void, void*) does to strcmp? Does it change the argument type to void and return type to int
@Simple It does nothing whatsoever to strcmp, for the same reason as int x=0; char* ptr = &x; does nothing to x. The pointer holds an address to the pointed-at item. Just because you let a pointer of a different type point to your variable, that doesn't change the type of the variable itself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.