如何接收无名结构中的C函数的参数? [英] How to receive unnamed structures as function parameters in C?

查看:204
本文介绍了如何接收无名结构中的C函数的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天经历<一会儿href=\"http://stackoverflow.com/questions/10031665/using-macros-in-c-to-define-data-structures/\">this的问题,我发现经过和接收无名结构作为函数参数的奇事。

Yesterday while going through this question, I found a curious case of passing and receiving unnamed structures as function parameters.

例如,如果我有这样的结构,

For example, if I have a structure like this,

int main ()
{
    struct {
        int a;
    } var;

    fun(&var);
}

现在,什么都要的乐趣原型是什么?我怎么能使用这种结构的为结构(指针)的在功能乐趣

Now, what should the prototype of fun be? And how can I use that structure as a structure(pointer) in the function fun?

推荐答案

有关调整的原因,这个原型应该工作:

For alignment reasons, this prototype should work:

void fun(int *x);

当然,

void fun(void *x);

我不明白一个简单的方法实际上是在功能,有效利用结构;也许该函数内部再次声明,然后分配无效*

6.7.2.1 - 15

一个指向结构对象,合适的转换,指向其
  初始成员
(如该成员是一个比特场,然后到单位
  它驻留),反之亦然。可能有不愿透露姓名的填充
  结构对象内,而不是在它的开头。

A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.

这篇关于如何接收无名结构中的C函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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