Difference between char and char[1]

后端 未结 3 1492
醉酒成梦
醉酒成梦 2020-12-24 01:37

In C++ what is the difference (if any) between using char and char[1].

examples:

struct SomeStruct
{
   char x;
   char y[1];
};

Do

3条回答
  •  悲&欢浪女
    2020-12-24 02:16

    If you've actually seen the construct char y[1] as the last member of a struct in production code, then it is fairly likely that you've encountered an instance of the struct hack.

    That short array is a stand-in for a real, but variable length array (recall that before c99 there was no such thing in the c standard). The programmer would always allocate such structs on the heap, taking care to insure that the allocation was big enough for the actual size of array that he wanted to use.

提交回复
热议问题