gpt4 book ai didi

c - 如何在 C 中接收未命名的结构作为函数参数?

转载 作者:行者123 更新时间:2023-12-01 23:25:04 24 4
gpt4 key购买 nike

昨天在经历this问题,我发现了一个将未命名结构作为函数参数传递和接收的奇怪案例。

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

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

fun(&var);
}

现在, fun 的原型(prototype)应该是什么?是?以及如何将该结构用作函数 fun 中的结构(指针) ?

最佳答案

出于对齐的原因,这个原型(prototype)应该可以工作:

void fun(int *x);
而且当然:
void fun(void *x);
我看不到在函数中实际有效使用结构的简单方法;也许在该函数中再次声明它,然后分配 void * ?
按要求编辑

6.7.2.1 - 15

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

关于c - 如何在 C 中接收未命名的结构作为函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10040369/

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