简体   繁体   English

在C中具有默认参数的函数指针

[英]Function pointer with default parameter in C

See, I have two functions, one to get a char, and other to put a char like this. 瞧,我有两个功能,一个是获取一个char,另一个是像这样放置一个char。

void function_get_char (input);
void function_put_char (output);

I have a function pointer to these functions like this. 我有一个指向这些函数的函数指针,就像这样。

void (*function_operators[])(input_or_output) = {function_get_char, function_put_char};

I need of, when I'll call my function_operator, I want to don't need to specify if I want to get it in of output or of my input. 我需要在调用function_operator时不需要指定是要从输出还是输入中获取它。

Do I need to build my function pointer like this? 我需要像这样构建函数指针吗?

void (*function_operators[])(input_or_output) = {function_get_char(input), function_put_char(output)};

How can I do it? 我该怎么做? Thanks in advance. 提前致谢。

NOTE 注意

input or output parameter, is not run_time parameter. 输入或输出参数,不是run_time参数。

I'm not really sure what you want to do, but if you want to fix input and output to some predefined value (since you say they are not runtime parameters), the easiest solution should be to write some wrapper functions that call the "real" functions with the correct parameters. 我不太确定要做什么,但是如果要将inputoutput固定为某个预定义值(因为您说它们不是运行时参数),最简单的解决方案应该是编写一些包装函数,这些包装函数调用“真正的”功能使用正确的参数。 For example: 例如:

void fixed_get_char(void) { function_get_char(input); }
void fixed_put_char(void) { function_put_char(output); }

void (*function_operators[])(void) = {fixed_get_char, fixed_put_char};

You will have to create a function with no arguments which wraps the call to the function with an argument. 您将必须创建一个不带参数的函数,该函数将对带参数的函数的调用包装起来。 If you need to do this at runtime, then look at (one of the definitions of) thunk or trampolines. 如果您需要在运行时执行此操作,则请查看thunk或蹦床(的定义之一)。

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

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