n**d 发帖数: 9764 | 1 1. why do both a[0] and a[1] work?
2. why do both a[0]('a') and (*a[0])('a') work?
#include
using namespace std;
typedef void FT(char);
typedef void (*FT_p)(char);
void func(char ch)
{ cout << "func is called by [" << ch << "]" << endl; }
int main()
{
FT *a[2];
a[0] = func;
a[0]('a');
(*a[0])('a');
a[1] = &func;
a[1]('a');
(*a[1])('a');
} |
r*********r 发帖数: 3195 | 2 there are implicit type conversions between a function name and
the corresponding function pointer both ways.
&, * are therefore both optional. |
P********e 发帖数: 2610 | 3
// this is short form.
// this is also the short form
// this is the correct syntax statement.
// this is the right form.
【在 n**d 的大作中提到】 : 1. why do both a[0] and a[1] work? : 2. why do both a[0]('a') and (*a[0])('a') work? : #include : using namespace std; : typedef void FT(char); : typedef void (*FT_p)(char); : void func(char ch) : { cout << "func is called by [" << ch << "]" << endl; } : int main() : {
|
n**d 发帖数: 9764 | 4 I see. Thanks!
We use typedef to declare FT as a type of pointer to function and then use
FT to declare a[2]. If we don't use typedef, how could we declare elements
of a[2] as pointers to functions?
【在 P********e 的大作中提到】 : : // this is short form. : // this is also the short form : // this is the correct syntax statement. : // this is the right form.
|
n**d 发帖数: 9764 | 5 got the answer.
void (*func_p_a[2])(char);
func_p_a[0] = func;
【在 n**d 的大作中提到】 : I see. Thanks! : We use typedef to declare FT as a type of pointer to function and then use : FT to declare a[2]. If we don't use typedef, how could we declare elements : of a[2] as pointers to functions?
|