Initializing a function pointer in C

帅比萌擦擦* 提交于 2019-12-10 13:43:57

问题


I have the function

uint8_t Authorization_getRole (char const* userId, UsertoRole_T const *roleTable)

and in the main program I have:

given_Role = Authorization_getRole (userId, roleTable)

I want to replace the function call with a function pointer:

uint8_t (*getRole_ptr)()

given_Role = &getRole_ptr;

My questions are:

Where do I initalize the function pointer getRole_ptr?

How do I initialize the function pointer?

Is the syntax below correct?

getRole_ptr = Authorization_getRole (userId, roleTable)

回答1:


I'd always recommend a typedef with function pointers. Then, you would write:

// Make sure to get the function's signature right here
typedef uint8_t (*GetRole_Ptr_T)(char const*, UsertoRole_T const*);

// Now initialize your pointer:
GetRole_Ptr_T getRole_ptr = Authorization_getRole;

// To invoke the function pointed to:
given_Role = getRole_ptr(userId, roleTable);

Regarding "Where do I initalize the function pointer getRole_ptr?": Depends on your requirements. You can do it when declaring the pointer, as I did in my example, or you can change the pointer later on by assigning to it:

getRole_ptr = Some_function_with_correct_signature;



回答2:


uint8_t (*getRole_ptr)()

The function pointer needs to have exactly the same format as the pointed at function. So you need to specify the types of the two parameters:

uint8_t (*getRole_ptr)(const char*, const UsertoRole_T*);

(Otherwise C will take your code to a horrible place called implicit land, where bizarre creatures like "default argument promotions" exist. You don't even want to know what that is, just write out the full function with proper parameters.)

Hopefully you can tell that the function pointer declaration just written looks like unreadable crap. Therefore you should use a typedef instead:

typedef uint8_t (*role_ptr_t)(const char*, const UsertoRole_T*);
...
role_ptr_t getRole_ptr;

Where do I initalize the function pointer getRole_ptr? How do I initialize the function pointer?

Formal initialization can only occur on the line where you declare a variable. So if you for some strange reason must use initialization, rather than assignment, then you have to do so on the same line as the declaration:

 role_ptr_t getRole_ptr = Authorization_getRole;

Is the syntax below correct?

No. See correct syntax above.




回答3:


You cannot replace a function call with a function pointer. You can call the pointer instead though:

uint8_t (*getRole_ptr)(char const*, UsertoRole_T const *)

given_Role = getRole_ptr( /* some args maybe */);

Initializing a function pointer:

uint8_t (*getRole_ptr)(char const*, UsertoRole_T const *) = Authorization_getRole;

A side note: when defining a function pointer you need to specify it's arguments.




回答4:


Normally you would define

uint8_t Authorization_getRole (char const* userId, UsertoRole_T const *roleTable);

in your header.

In a code block then... (see comments)

int main(int argc, const char * argv[])
{
    uint8_t x;

    // define "func" as a pointer to a function with specific arguments & return value

    uint8_t (*func)(char const* userId, UsertoRole_T const *roleTable);

    // point "func" to the function

    func = &Authorization_getRole;

    // you can use "func" as a variable or invoke it

    x = func( "FOO", NULL);

}



回答5:


The variable declaration should be:

uint8_t(*getRole_ptr)(char const*, UsertoRole_T const *);

You assign it with:

getRole_ptr = Authorization_getRole;

And you call it with:

given_Role = getRole_ptr(userId, roleTable);


来源:https://stackoverflow.com/questions/23269710/initializing-a-function-pointer-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!