C - 在没有参数的情况下调用用参数声明的函数?
C - Calling a function declared with parameters with no parameters?
我正在尝试理解具有以下几行的代码:
void terminate_pipe(int);
code code code...
struct sigaction new_Sigiterm;
new_Sigiterm.sa_handler = terminate_pipe;
我的问题是:
这样调用一个函数是什么意思?是不是要
只是把 NULL
作为参数?
是无效的,所以new_Sigiterm.sa_handler
无论如何都会是NULL
?
谢谢。
像这样赋值的代码正在设置一个处理程序(有时称为 函数指针 ):基本上,在给定时间 运行 的函数地址。
C 中的语法是为函数命名,但不要将 ()
放在末尾。即returns函数的地址。
new_Sigiterm.sa_handler = terminate_pipe;
void terminate_pipe(int);
不是函数的调用它是函数的 Forward declaration。
- 在
new_Sigiterm.sa_handler
中sa_handler
是一个Function Pointer.
new_Sigiterm.sa_handler
很可能是指向函数的指针。通过 运行宁
new_Sigiterm.sa_handler = terminate_pipe;
类似于说
new_Sigiterm.sa_handler = &terminate_pipe;
(就像指针一样)。这不是 运行 函数,它只是创建一个指向函数的指针,如果你 "run" 指针,指向的函数将 运行.
这是声明函数指针的方法:
void function(int x);
int main()
{
//Pointer to function
void (*foo) (int);
//Point it to our function
foo = function;
//Run our pointed function
foo(5);
}
我正在尝试理解具有以下几行的代码:
void terminate_pipe(int);
code code code...
struct sigaction new_Sigiterm;
new_Sigiterm.sa_handler = terminate_pipe;
我的问题是:
这样调用一个函数是什么意思?是不是要 只是把
NULL
作为参数?是无效的,所以
new_Sigiterm.sa_handler
无论如何都会是NULL
?
谢谢。
像这样赋值的代码正在设置一个处理程序(有时称为 函数指针 ):基本上,在给定时间 运行 的函数地址。
C 中的语法是为函数命名,但不要将 ()
放在末尾。即returns函数的地址。
new_Sigiterm.sa_handler = terminate_pipe;
void terminate_pipe(int);
不是函数的调用它是函数的 Forward declaration。- 在
new_Sigiterm.sa_handler
中sa_handler
是一个Function Pointer.
new_Sigiterm.sa_handler
很可能是指向函数的指针。通过 运行宁
new_Sigiterm.sa_handler = terminate_pipe;
类似于说
new_Sigiterm.sa_handler = &terminate_pipe;
(就像指针一样)。这不是 运行 函数,它只是创建一个指向函数的指针,如果你 "run" 指针,指向的函数将 运行.
这是声明函数指针的方法:
void function(int x);
int main()
{
//Pointer to function
void (*foo) (int);
//Point it to our function
foo = function;
//Run our pointed function
foo(5);
}