gpt4 book ai didi

c - 这两个函数指针声明有什么区别?

转载 作者:行者123 更新时间:2023-11-30 20:15:01 25 4
gpt4 key购买 nike

int *(*const fun[])(int argc, char **argv)

const int *(* fun[])(int argc, char **argv).

第一个是返回整数指针的 const 函数指针数组吗?

最佳答案

第一个是一个只读指针数组(即,您不能更改 fun[i])到接收 int 的函数>char **,并返回一个指向 int 的指针。

第二个非常相似,只是您可以更改 fun[i],但它指向的函数返回一个指向只读整数的指针。

所以,简而言之:

/* First declaration 
int *(*const fun[])(int argc, char **argv)
*/
int arg1;
char **arg2;
int *example = (*fun[i])(arg1, arg2);
*example = 14; /* OK */
example = &arg1; /* OK */
fun[i] = anoter_function; /* INVALID - fun[] is an array of read-only pointers */

/* Second declaration
const int *(* fun[])(int argc, char **argv)
*/
const int *example2 = (*fun[i])(arg1, arg2);
fun[i] = another_function; /* OK */
*example2 = 14; /* INVALID - fun[i] returns pointer to read-only value. */
example2 = &arg1; /* OK */

关于c - 这两个函数指针声明有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22374754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com