为什么在初始化多维数组时我不能完全省略维度?

时间:2021-05-26 21:35:39

In Visual Studio 2010, this initialization works as expected:

在Visual Studio 2010中,此初始化按预期工作:

char table[2][2] = {
                       {'a', 'b'},
                       {'c', 'd'}
                   };

But it does not seem legal to write something like:

但是写下这样的东西似乎不合法:

char table[][] = {
                     {'a', 'b'},
                     {'c', 'd'}
                 };

Visual Studio complains that this array may not contain elements of 'that' type, and after compiling, VS reports two errors: a missing index and too many initializations.

Visual Studio抱怨此数组可能不包含'that'类型的元素,并且在编译之后,VS报告两个错误:缺少索引和太多初始化。

QUESTION: Why can't I omit the dimensions altogether when initializing a multi-dimensional array?

问题:为什么在初始化多维数组时我不能完全省略维度?

2 个解决方案

#1


13  

Only the innermost dimension can be omitted. The size of elements in an array are deduced for the type given to the array variable. The type of elements must therefore have a known size.

只能省略最内层的尺寸。对于给定数组变量的类型,推导出数组中元素的大小。因此,元素的类型必须具有已知的大小。

  • char a[]; has elements (e.g. a[0]) of size 1 (8bit), and has an unknown size.
  • char a [];具有大小为1(8位)的元素(例如a [0]),并且具有未知的大小。

  • char a[6]; has elements of size 1, and has size 6.
  • char a [6];具有大小为1的元素,大小为6。

  • char a[][6]; has elements (e.g. a[0], which is an array) of size 6, and has an unknown size.
  • char a [] [6];具有大小为6的元素(例如,[0],它是一个数组),并且具有未知的大小。

  • char a[10][6]; has elements of size 6. and has size 60.
  • char a [10] [6];具有大小为6的元素,大小为60。

Not allowed:

  • char a[10][]; would have 10 elements of unknown size.
  • char a [10] [];将有10个未知大小的元素。

  • char a[][]; would have an unknown number of elements of unknown size.
  • char a [] [];将有未知数量的未知大小的元素。

The size of elements is mandatory, the compiler needs it to access elements (through pointer arithmetic).

元素的大小是必需的,编译器需要它来访问元素(通过指针算术)。

#2


0  

Is this an acceptable work-around?

这是一个可接受的解决方案吗?

char * table [] = { "ab", "cd" };

EDIT: Note that it will add an extra '\0' on the end of each string.

编辑:请注意,它会在每个字符串的末尾添加一个额外的'\ 0'。

#1


13  

Only the innermost dimension can be omitted. The size of elements in an array are deduced for the type given to the array variable. The type of elements must therefore have a known size.

只能省略最内层的尺寸。对于给定数组变量的类型,推导出数组中元素的大小。因此,元素的类型必须具有已知的大小。

  • char a[]; has elements (e.g. a[0]) of size 1 (8bit), and has an unknown size.
  • char a [];具有大小为1(8位)的元素(例如a [0]),并且具有未知的大小。

  • char a[6]; has elements of size 1, and has size 6.
  • char a [6];具有大小为1的元素,大小为6。

  • char a[][6]; has elements (e.g. a[0], which is an array) of size 6, and has an unknown size.
  • char a [] [6];具有大小为6的元素(例如,[0],它是一个数组),并且具有未知的大小。

  • char a[10][6]; has elements of size 6. and has size 60.
  • char a [10] [6];具有大小为6的元素,大小为60。

Not allowed:

  • char a[10][]; would have 10 elements of unknown size.
  • char a [10] [];将有10个未知大小的元素。

  • char a[][]; would have an unknown number of elements of unknown size.
  • char a [] [];将有未知数量的未知大小的元素。

The size of elements is mandatory, the compiler needs it to access elements (through pointer arithmetic).

元素的大小是必需的,编译器需要它来访问元素(通过指针算术)。

#2


0  

Is this an acceptable work-around?

这是一个可接受的解决方案吗?

char * table [] = { "ab", "cd" };

EDIT: Note that it will add an extra '\0' on the end of each string.

编辑:请注意,它会在每个字符串的末尾添加一个额外的'\ 0'。