函数指针中是否有星号可选? [英] Is an asterisk optional in a function pointer?
问题描述
我使用的代码有这样的声明:
$ b $ pre $ typedef void(udp_data_notify)(OS_FIFO * pfifo,WORD port);
这看起来像是udp_data_notify函数指针的声明,但没有 *
。它可以仍然是一个没有星号的函数指针吗?
这是一条使用 udp_data_notify
的语句:
void RegisterUDPFifoWithNotify(WORD dp,OS_FIFO * pnewfifo,udp_data_notify * nudp)
一个typedef,如:
typedef void name(int);
( name
>的括号是多余的)将定义 name
作为函数的类型,而不是函数指针。那将是:
typedef void(* pname)(int);
您可能想知道它们的优点。那么,函数类型并不是非常有用,除了声明指针外:
name * pointer_to_function;
使用其他typedef可以使其更具可读性:
pname pointer_to_function;
这是因为您无法定义函数类型的变量。如果你尝试了,你只需要编写一个函数的原型,但是使用的语法很模糊:
//声明(原型),而不是变量void foo(int x)//定义
{
}
但是请注意,您不能使用typedef来定义函数:
$ $ p $ name foo {} //语法错误!
The code I am using has this statement:
typedef void ( udp_data_notify )(OS_FIFO * pfifo, WORD port);
This looks like a declaration of a function pointer for udp_data_notify, however there is no *
. Can it still be a function pointer without an asterisk?
Here is a statement that uses udp_data_notify
:
void RegisterUDPFifoWithNotify( WORD dp, OS_FIFO *pnewfifo , udp_data_notify * nudp)
Any help as to what is happening would be appreciated!
A typedef such as:
typedef void name(int);
(the parenthesis around name
are redundant) will define name
as the type of a function, not a function pointer. That would be:
typedef void (*pname)(int);
You are probably wondering what they are good for. Well, function types are not very useful, other than for declaring pointers:
name *pointer_to_function;
And that can be made arguably more readable with the other typedef:
pname pointer_to_function;
That's because you cannot define a variable of type function. If you try, you will simply write the prototype of a function, but in a quite obfuscated syntax:
name foo; //declaration (prototype), not variable
void foo(int x) //definition
{
}
But note that you cannot use the typedef to define the function:
name foo {} //syntax error!
这篇关于函数指针中是否有星号可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!