简体   繁体   English

指向 C 中前向声明结构数组的指针

[英]A pointer to array of forward declared structures in C

I have a question about forward declarations in C.我有一个关于 C 中的前向声明的问题。

Code代码

typedef struct yhash_s t_yhash;// forward declaration
struct yhash_s {
    size_t  size_h; 
    t_yhash (*yhash)[];// pointer to array of structures
};

Once the code is compiled with gcc, it complains:使用 gcc 编译代码后,它会抱怨:

error: array type has incomplete element type 't_yhash' {aka 'struct yhash_s'}错误:数组类型具有不完整的元素类型 't_yhash' {aka 'struct yhash_s'}

I do understand that t_yhash is not know (yet) and size of array can't be computed, but I am asking about pointer to an array of unknown yet size, which should be perfectly resolvable IMHO.我确实知道t_yhash不知道(还)并且无法计算数组的大小,但我问的是指向大小未知的数组的指针,恕我直言,这应该是完全可解析的。

How do I fix that forward declaration and struct itself?如何修复该前向声明和结构本身?

The trouble is that array declarators may not have an incomplete type as the element type (C11 6.7.6.2/1).问题是数组声明符可能没有不完整的类型作为元素类型(C11 6.7.6.2/1)。 And t_yash (ie struct yhash_s ) is not complete until the closing brace of the struct definition.并且t_yash (即struct yhash_s )直到结构定义的struct yhash_s大括号才完整。

This rule is also responsible for another piece of trivia;这条规则还负责另一个琐事; it's legal to have (before the struct definition is complete):拥有(在结构定义完成之前)是合法的:

void func( t_yhash *a );

but not legal to have:但不合法:

void func( t_yhash a[] );

even though the adjustment rule would work just fine if not for the incomplete element type rule.即使如果不是不完整的元素类型规则,调整规则也能正常工作。

Probably the language design could be improved slightly by refining this rule to allow some cases like the function prototype, but it clearly wasn't something that arose with the language design committee.或许可以通过细化这个规则来稍微改进语言设计,以允许像函数原型这样的一些情况,但这显然不是语言设计委员会提出的。

But even without this rule, your use case might have another problem;但即使没有这条规则,您的用例也可能有另一个问题; the size of the pointer might not be known.指针的大小可能未知。 It would be legal (although unlikely in practice) for "pointer to array of struct X" to have a different size than "pointer to array of struct Y". “指向结构 X 数组的指针”与“指向结构 Y 数组的指针”的大小不同是合法的(尽管在实践中不太可能)。 There is a rule that all pointers to struct must have the same size, but no such rule for pointers to array.有一条规则是所有指向 struct 的指针必须具有相同的大小,但对于指向数组的指针没有这样的规则。

In reply to this part of your post:回复您帖子的这一部分:

How do I fix that forward declaration and struct itself?如何修复该前向声明和结构本身?

You can use void * to stash your array, and then convert it back later.您可以使用void *来存储您的数组,然后再将其转换回来。

typedef struct yhash_s t_yhash;
struct yhash_s {
               size_t  size_h;
               void *yhash;
               };

static inline t_yhash (*yhash(t_yhash y))[] {
    return y.yhash;
}

If the function syntax is too obtuse:如果函数语法太钝:

typedef t_yhash t_yhash_array[];

static inline t_yhash_array *yhash(t_yhash y) {
    return y.yhash;
}

For example:例如:

t_yhash x[10];
t_yhash y = { 10, &x };
assert(yhash(y) == &x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM