假设我有以下类型的结构:如何创建结构数组,其中结构的最后一个元素是一个结构破解
struct employee
{
int emp_id;
int name_len;
char name[0];
};
我知道
struct employee *e = malloc(sizeof(*e) + sizeof(char) * 128);
相当于到
struct employee
{
int emp_id;
int name_len;
char name[128]; /* character array of size 128 */
};
我的问题是我可以创建一个这样的结构数组,当结构中的最后一个元素是st破坏黑客。
例如:我想创建一个结构employee [3]的数组;使得
雇员[0]可以等同于
struct employee
{
int emp_id;
int name_len;
char name[128]; /* character array of size 128 */
};
雇员[1]可以等同于
struct employee
{
int emp_id;
int name_len;
char name[256]; /* character array of size 256 */
};
雇员[2]可以等同于
struct employee
{
int emp_id;
int name_len;
char name[512]; /* character array of size 512*/
};
以'0'作为大小的struct hack不符合C.由于C99我们有“灵活的数组成员”,所以它们用'[]'声明而没有任何大小。 – 2013-02-13 07:00:53
[在Linux内核代码中有零元素的数组需要什么?]可能的重复(http://stackoverflow.com/questions/14643406/whats-the-need-of-array-with-zero-element-in- Linux内核代码)。这篇文章回答了这个问题,只是忽视了Linux相关的一切。 – Lundin 2013-02-13 07:38:55