且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-20 23:32:46


  

是指定义一个名为哪些功能XXX


块引用>

它定义一个函数的键入


  

F(XXX a)是相同的为f(XXX * a)个


块引用>

是的。但是,这是排序的语言黑客。星只是帮忙,为你添加,因为它失踪了。


  

的sizeof(someFunction)被定义或没有?


块引用>

它具体是不是。你可以,或使用你的程序是如何联系在一起的亲密知识与汇编语言做到这一点,但它不具有任何语义含义C.功能,甚至不需要被寻址超越间接跳转,如C支持哈佛架构的机器。 (另请注意,用相同类型的两个函数通常将具有不同的大小,如尺寸是由它的指令的数量和性质决定的。)

C11 6.3.2.1/4说:


  

一个函数标志是具有函数型的前pression。除非该值是sizeof操作符的操作数,...一个功能标志型'函数返回类型''转换到具有类型'指针函数返回类型''一个前pression。


块引用>

和6.5.3.4/1说:


  

sizeof运算符不得应用于具有函数型的前pression ...


块引用>

看来你的编译器未能申请6.3.2.1/4。需要一条错误消息。

typedef int (xxx)(int yyy); means define a function which named xxx with an integer parameter. you can see this SO post for details.

I have tried this in different ways, that is my code:

#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));
}

output:

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

My question:

  1. f(xxx a) is the same as f(xxx *a)?
  2. sizeof(someFunction) is defined or not?

means define a function which named xxx

It defines a function type.

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

Yes. But this is sort of a hack in the language. The star is just helpfully added for you because it was missing.

sizeof(someFunction) is defined or not?

It specifically is not. You can do this with in assembly language, or by using intimate knowledge of how your program is linked, but it has no semantic meaning to C. Functions don't even need to be addressable beyond indirect jumps, as C supports Harvard architecture machines. (Also, note that two functions with the same type will usually have different sizes, as the size is determined by the number and nature of its instructions.)

C11 6.3.2.1/4 says

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’’.

and 6.5.3.4/1 says

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

It appears that your compiler is failing to apply 6.3.2.1/4. An error message is required.