所以有一个问题是:
int a[2][2][2] = {{5,6},{7,8},{9,10},{11,12}};
写一个打印出“7910”的printf语句.使用指针而不使用方括号.
起初,我认为这是一个错字或非法阵列.我以为数组应该从左边的第三个数组停止.
我写:
printf("%d%d%d\n",*(*(a+1)+1)),*(*(a+2)),*(*(a+2)));
我把它放了,因为如果阵列是
int a[2][2] = {{7,12}};
类似的语法会起作用.
这是一个错字吗?如果没有,那么正确的语法是什么?
解决方法
int a[2][2][2] = { { {5,8} },{ {9,12} } };
如果您将其写为,这也将起作用
int a[2][2][2] = {5,6,7,8,9,10,11,12};
但这确实不是一个好习惯.
C标准说的
§6.7.9(P17):
When no designations are present,subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order,structure members in declaration order,and the first named member of a union149). […]
P26
EXAMPLE 3 The declaration
06002
is a definition with a fully bracketed initialization:
1
,3
,and5
initialize the first row ofy
(the array objecty[0]
),namelyy[0][0]
,y[0][1]
,andy[0][2]
. Likewise the next two lines initializey[1]
andy[2]
. The initializer ends early,soy[3]
is initialized with zeros. Precisely the same effect could have been achieved by06003
The initializer for
y[0]
does not begin with a left brace,so three items from the list are used. Likewise the next three are taken successively fory[1]
andy[2]
.