一个函数的typedef作为另一个函数的参数 [英] A typedef function as a parameter of another function

查看:391
本文介绍了一个函数的typedef作为另一个函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的typedef INT(XXX)(INT YYY); 是指定义一个名为哪些XXX一个整数参数的函数。你可以看到这 SO帖子了解详情。

我已经以不同的方式尝试了这一点,这是我的code:

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
的typedef INT(XXX)(INT YYY);无效F1(XXX一)
{
    的printf(F1数:%d \\ n,一);
}
无效F2(XXX *一)
{
    的printf(F2数:%d \\ n,一);
}
INT测试(int y)对
{
}
诠释的main()
{
    XXX *一个;
    F1(试验);
    F1(A);
    F2(试验);
    F2(一);    XXX B:
    的printf(XXX的大小数:%d \\ n,sizeof的(B));
}

输出:

  F1:4199274
F1:2
F2:4199274
F2:2
XXX的大小:1

我的问题:


  1. F(XXX A)相同 F(XXX * A)

  2. 的sizeof(someFunction)定义或没有?


解决方案

  

是指定义一个名为哪些功能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.

这篇关于一个函数的typedef作为另一个函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆