What is __flexarr and how/why do c programmers use it?

前端 未结 1 2095
Happy的楠姐
Happy的楠姐 2020-12-18 02:09

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

struct inotify_event
{
  int wd;               /* Watch descriptor.  */
  uint32_t ma         


        
1条回答
  •  庸人自扰
    2020-12-18 02:56

    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.

    The sys/cdefs.h I have says:

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

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

    0 讨论(0)
提交回复
热议问题