简体   繁体   English

数组初始化[c / c ++]

[英]Array initialization [c/c++]

Why this is not allowed? 为什么不允许这样做?

#include <cstdio>

struct Foo {
int fooId;
char arr[ ];
} fooList[] =
{ {1, {'a', 'b'}},
  {2, {'c', 'd'}}
};

int main()
{
  for (int i = 0; i < 2; i++)
    printf("%d %c\n", fooList[i].fooId, fooList[i].arr[0]);
}

whereas, this is allowed: 然而,这是允许的:

struct Foo {
int fooId;
char arr[2]; // this line modified
} fooList[] =
{ {1, {'a', 'b'}},
  {2, {'c', 'd'}}
};

Only the last member of a C struct can be flexible as in arr[] . 只有C结构的最后一个成员可以像arr[]一样灵活。

Shamelessly copying from paragraph 6.7.2.1, sub-paragraph 16 of the ISO C99 standard: 无耻地复制ISO C99标准第6.7.2.1段第16段:

16 As a special case, the last element of a structure with more than one named member may have an incomplete array type; 16作为一种特殊情况,具有多个命名成员的结构的最后一个元素可能具有不完整的数组类型; this is called a flexible array member. 这被称为灵活的阵列成员。 With two exceptions, the flexible array member is ignored. 除了两个例外,灵活的数组成员将被忽略。 First, the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length.106)... 首先,结构的大小应该等于其他相同结构的最后一个元素的偏移量,该结构用一个未指定长度的数组替换柔性数组成员.106)...

EDIT: 编辑:

As for C++, see this . 至于C ++,请看这个 Bottom-line: flexible array members are not allowed in C++ at all - at least for the time being. 底线:C ++中根本不允许使用灵活的数组成员 - 至少目前是这样。

In C++ all members of an user defined type must have complete types, and the member arr does not have a complete type unless you give it a size. 在C ++中,用户定义类型的所有成员必须具有完整类型,并且成员arr没有完整类型,除非您为其指定大小。

In C, the struct definition would compile, but you might not get what you want. 在C中,struct定义会编译,但是你可能得不到你想要的。 The problem is that an array without size is allowed at the end of a struct to be used as a proxy to access the contiguous block of memory after the instance. 问题是在结构的末尾允许没有大小的数组用作代理,以在实例之后访问连续的内存块。 This allows a dumb vector implementation as: 这允许哑向量实现为:

typedef struct vector {
   int size;
   char buffer[];
} vector;
vector* create_vector( int size ) {
   vector* p = (vector*) malloc( sizeof *p + size ); // manually allocate "size" extra
   p->size = size;
};
int main() {
   vector* v = create_vector(10);
   for ( int i = 0; i < v->size; ++i )
      printf("%d\n", v->buffer[i] );
   free(v);
}

But the language does not allow you to initialize with the curly braces as the compiler does not know how much memory has to be held (in general, in some circumstances it can know). 但是该语言不允许您使用花括号进行初始化,因为编译器不知道需要保留多少内存(通常,在某些情况下它可以知道)。 The size-less member of the struct is only a way of accessing beyond the end of the object, it does not hold memory in itself: struct的size-less成员只是一种超出对象末尾的访问方式,它本身不包含内存:

printf( "sizeof(vector)=%d\n", sizeof(vector) ); // == sizeof(int)

In C++03, this is not allowed in struct or class! 在C ++ 03中,结构或类中不允许这样做!

Comeau C++ compiler gives this error: Comeau C ++编译器给出了这个错误:

 "ComeauTest.c", line 3: error: “ComeauTest.c”,第3行:错误:  \nincomplete type is not allowed 不允许不完整的类型 \n      char arr[ ]; char arr [];\n           ^ ^ 

Exactly simlar question yesterday : Difference between int* and int[] in C++ 昨天完全类似的问题: C ++中int *和int []之间的区别

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

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