什么是__flexarr如何/为什么C程序员使用它呢? [英] What is __flexarr and how/why do c programmers use it?

查看:598
本文介绍了什么是__flexarr如何/为什么C程序员使用它呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SYS / inotify.h (<一个href=\"https://android.googlesource.com/platform/$p$pbuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/+/android-4.1.1_r6/sysroot/usr/include/sys/inotify.h\"相对=nofollow> inotify.h ),下面的结构定义:

In sys/inotify.h (inotify.h), following struct is defined:

struct inotify_event
{
  int wd;               /* Watch descriptor.  */
  uint32_t mask;        /* Watch mask.  */
  uint32_t cookie;      /* Cookie to synchronize two events.  */
  uint32_t len;         /* Length (including NULs) of name.  */
  char name __flexarr;  /* Name.  */
};

我找不到 __ flexarr 在code的任何定义。我会在哪里寻找呢?

I can't find any definitions for __flexarr in the code. Where would I search for it?

在一个不相关的项目中,我发现的#define __flexarr [1] 我假设有类似的功能,但这个定义并没有多大意义,我(是与相当陌生C)。

In an unrelated project I found #define __flexarr [1] which I assume does something similar, but this definition does not make much sense to me (being quite unfamiliar with C).

我唯一知道的是,你可以存储各种长度的字符串,但我完全缺少的是如何/为什么这个工作的理解。

The only thing I know is that you can store strings of various length in it, but I completely lack the understanding of how/why this works.

是否有人照顾解释?谢谢你。

Does anybody care to explain? Thanks.

推荐答案

这是结构黑客。 C99正式加入它柔性数组成员的形式。

It's the struct hack. C99 added it officially in the form of "flexible array member".

作为一个特例,一个结构的多于一个的最后一个元件
  名为构件可以具有一个不完整的数组类型;这就是所谓的
  灵活的数组成员。在大多数情况下,该柔性阵列构件
  被忽略。具体地,结构的尺寸是因为如果
  除了它可能有更灵活的阵列成员已被删去
  尾随填充比遗漏意味着

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply.

SYS / cdefs.h 我说:

#if __GNUC_PREREQ (2,97)
/* GCC 2.97 supports C99 flexible array members.  */
# define __flexarr      []
#else
# ifdef __GNUC__
#  define __flexarr     [0]
# else
....

如果你正在写新的code,使用标准规定的方式,它是只是一个 []

If you are writing new code, the standard-mandated way to use it is with just a [].

这篇关于什么是__flexarr如何/为什么C程序员使用它呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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