简体   繁体   English

在C中初始化函数指针

[英]Initializing a function pointer in C

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? 我在哪里初始化函数指针getRole_ptr?

How do I initialize the function pointer? 如何初始化函数指针?

Is the syntax below correct? 下面的语法是否正确?

getRole_ptr = Authorization_getRole (userId, roleTable)

I'd always recommend a typedef with function pointers. 我总是推荐一个带有函数指针的typedef。 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. 关于“我在哪里初始化函数指针getRole_ptr?”:取决于您的要求。 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;

uint8_t (*getRole_ptr)() 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.) (否则C会把你的代码带到一个叫做隐含土地的可怕地方,那里存在像“默认参数促销”这样的怪异生物。你甚至不想知道它是什么,只需用适当的参数写出完整的函数。)

Hopefully you can tell that the function pointer declaration just written looks like unreadable crap. 希望您可以告诉刚刚编写的函数指针声明看起来像不可读的废话。 Therefore you should use a typedef instead: 因此,您应该使用typedef:

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? 我在哪里初始化函数指针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. 不。请参阅上面的正确语法

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. 附注:在定义函数指针时,需要指定它的参数。

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);

}

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM