功能指针,(* f)和f之间有什么区别

时间:2021-06-25 13:25:18

I have a an array of int and I need to assign a function pointer,

我有一个int数组,我需要分配一个函数指针,

f(tab[i]) and (*f)(tab[i])

What is the difference between those 2 syntax ??

那两个语法有什么区别?

2 个解决方案

#1


2  

Your code looks as if you are invoking a function via a function pointer, and you're seeking to pass one element of an array to that function. The pointer might be defined as:

您的代码看起来好像是通过函数指针调用函数,并且您正在寻求将数组的一个元素传递给该函数。指针可能定义为:

void (*f)(int);     // Function pointer — guessed argument and return types

The difference between the invocations (*f)(tab[i]) and f(tab[i]) is spelling and history — the result is the same.

调用(* f)(tab [i])和f(tab [i])之间的区别在于拼写和历史记录 - 结果是相同的。

The (*f)(tab[i]) notation was mandatory before the C90 standard; the f(tab[i]) notation was allowed by C90. The advantage of (*f)(tab[i]) is that you don't accidentally head off looking for a function f — you can see it's a function pointer immediately. (Of course, that assumes you don't have a colleague who delights in writing (*sqrt)((*sin)(x)*(*cos)(x)) or other similar valid but unconscionably obscure absurdities.)

在C90标准之前,(* f)(tab [i])符号是强制性的; C90允许使用f(tab [i])表示法。 (* f)(tab [i])的优点是你不会意外地寻找函数f - 你可以立即看到它是一个函数指针。 (当然,假设你没有一位喜欢写作的同事(* sqrt)((* sin)(x)*(* cos)(x))或其他类似的有效但不合情理的荒谬荒谬。)

#2


1  

If tab is an array of integer, then this below statement

如果tab是一个整数数组,那么下面的语句

(*f)(tab[i])

is invoking a function using function pointer, where f is a function pointer which can points to a function which takes argument as tab[i]. for e.g In below code block

使用函数指针调用函数,其中f是一个函数指针,它可以指向一个函数,该函数将参数作为tab [i]。例如,在下面的代码块中

void print_arr(int arr_ele_value) {
        printf("%d\t",arr_ele_value);
}
int main(void) {
        int arr[] = {1,2,3,4,5},ele;
        ele = sizeof(arr)/sizeof(arr[0]);
        void (*f)(int); /* function pointer declaration */
        f = print_arr; /* fun-ptr initialization */
        for(int row = 0; row < ele; row++) {
                (*f)(arr[row]); /* calling a function through function pointer */
        }
        return 0;
}

This

这个

void (*f)(int);

is function pointer declaration which says, f is a function pointer which can points to a function which takes input argument as int and returns void.

是函数指针声明,它表示,f是一个函数指针,它可以指向一个函数,它将输入参数作为int并返回void。

And here

和这里

f = function;

f is assigned with function which satisfies above condition of function-pointer declaration.

f被赋予满足上述函数指针声明条件的函数。

And this is how you can call a function using function pointer

这就是你如何使用函数指针调用函数

(*f)(arr[row]);

#1


2  

Your code looks as if you are invoking a function via a function pointer, and you're seeking to pass one element of an array to that function. The pointer might be defined as:

您的代码看起来好像是通过函数指针调用函数,并且您正在寻求将数组的一个元素传递给该函数。指针可能定义为:

void (*f)(int);     // Function pointer — guessed argument and return types

The difference between the invocations (*f)(tab[i]) and f(tab[i]) is spelling and history — the result is the same.

调用(* f)(tab [i])和f(tab [i])之间的区别在于拼写和历史记录 - 结果是相同的。

The (*f)(tab[i]) notation was mandatory before the C90 standard; the f(tab[i]) notation was allowed by C90. The advantage of (*f)(tab[i]) is that you don't accidentally head off looking for a function f — you can see it's a function pointer immediately. (Of course, that assumes you don't have a colleague who delights in writing (*sqrt)((*sin)(x)*(*cos)(x)) or other similar valid but unconscionably obscure absurdities.)

在C90标准之前,(* f)(tab [i])符号是强制性的; C90允许使用f(tab [i])表示法。 (* f)(tab [i])的优点是你不会意外地寻找函数f - 你可以立即看到它是一个函数指针。 (当然,假设你没有一位喜欢写作的同事(* sqrt)((* sin)(x)*(* cos)(x))或其他类似的有效但不合情理的荒谬荒谬。)

#2


1  

If tab is an array of integer, then this below statement

如果tab是一个整数数组,那么下面的语句

(*f)(tab[i])

is invoking a function using function pointer, where f is a function pointer which can points to a function which takes argument as tab[i]. for e.g In below code block

使用函数指针调用函数,其中f是一个函数指针,它可以指向一个函数,该函数将参数作为tab [i]。例如,在下面的代码块中

void print_arr(int arr_ele_value) {
        printf("%d\t",arr_ele_value);
}
int main(void) {
        int arr[] = {1,2,3,4,5},ele;
        ele = sizeof(arr)/sizeof(arr[0]);
        void (*f)(int); /* function pointer declaration */
        f = print_arr; /* fun-ptr initialization */
        for(int row = 0; row < ele; row++) {
                (*f)(arr[row]); /* calling a function through function pointer */
        }
        return 0;
}

This

这个

void (*f)(int);

is function pointer declaration which says, f is a function pointer which can points to a function which takes input argument as int and returns void.

是函数指针声明,它表示,f是一个函数指针,它可以指向一个函数,它将输入参数作为int并返回void。

And here

和这里

f = function;

f is assigned with function which satisfies above condition of function-pointer declaration.

f被赋予满足上述函数指针声明条件的函数。

And this is how you can call a function using function pointer

这就是你如何使用函数指针调用函数

(*f)(arr[row]);