gpt4 book ai didi

c - 作为另一个函数的参数的 typedef 函数

转载 作者:太空狗 更新时间:2023-10-29 17:21:27 24 4
gpt4 key购买 nike

typedef int (xxx)(int yyy); 表示定义一个名为 xxx 的函数,参数为整型。您可以查看此 SO post 以获取详细信息。

我以不同的方式尝试过,这是我的代码:

#include<stdio.h>
#include<stdlib.h>
typedef int (xxx)(int yyy);

void f1(xxx a)
{
printf("f1:%d\n",a);
}
void f2(xxx *a)
{
printf("f2:%d\n",a);
}
int test(int y)
{
}
int main()
{
xxx *a;
f1(test);
f1(a);
f2(test);
f2(a);

xxx b;
printf("xxx's size:%d\n", sizeof(b));
}

输出:

f1:4199274
f1:2
f2:4199274
f2:2
xxx's size:1

我的问题:

  1. f(xxx a)f(xxx *a) 一样吗?
  2. sizeof(someFunction) 是否定义?

最佳答案

means define a function which named xxx

它定义了一个函数type

f(xxx a) is the same as f(xxx *a)?

是的。但这是一种对语言的破解。星号只是帮助您添加,因为它丢失了。

sizeof(someFunction) is defined or not?

具体不是。您可以使用汇编语言或通过使用对程序链接方式的深入了解来执行此操作,但它对 C 没有语义意义。函数甚至不需要在间接跳转之外可寻址,因为 C 支持哈佛体系结构机器。 (另请注意,具有相同类型的两个函数通常具有不同的大小,因为大小由其指令的数量和性质决定。)

C11 6.3.2.1/4 说

A function designator is an expression that has function type. Except when it is the operand of the sizeof operator, … a function designator with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’.

和 6.5.3.4/1 说

The sizeof operator shall not be applied to an expression that has function type…

您的编译器似乎无法应用 6.3.2.1/4。需要错误消息。

关于c - 作为另一个函数的参数的 typedef 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18456810/

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