Are these three-dimensional arrays legal in C?

My alumni exam consisted of a very complex syntax. For example, some of his questions were like “use pointer 1 to print the letter k without using brackets. Fortunately, it was an open book.

So one question:

int a[2][2][2] = {{5,6}, {7,8}, {9,10}, {11,12}}; 

write a printf statement that prints "7910". Using pointers without using square brackets.

At first I thought it was a typo or an illegal array. I thought the array should stop in the third array on the left.

I wrote:

printf("%d%d%d\n",*(*(a+1)+1)),*(*(a+2)),*(*(a+2)));

I put this because if the array was

int a[2][2] = {{7,8},{11,12}};

similar syntax will work.

So it was a typo? If not, what is the correct syntax?

+6
2

, ,

int a[2][2][2] = { { {5,6}, {7,8} }, 
                   { {9,10}, {11,12} }
                 };   

,

int a[2][2][2] = {5, 6, 7, 8, 9, 10, 11, 12};   

.

C
§6.7.9 (17):

, : , 149). [...]

p26

3

int y[4][3] = {
    { 1, 3, 5 },
    { 2, 4, 6 },
    { 3, 5, 7 },
};

- : 1, 3 5 y ( y[0]), y[0][0], y[0][1], y[0][2]. , y[1] y[2]. , y[3] .

int y[4][3] = {
    1, 3, 5, 2, 4, 6, 3, 5, 7
};

y[0] , . , y[1] y[2].

+5

:

int a[2][2][2] = {{5,6}, {7,8}, {9,10}, {11,12}}; 

C, .

C11 N1570, §6.7.9/20 :

, , . subaggregate , , , .

, {{5,6}, {7,8}, {9,10}, {11,12}} (.. a int), :

int a[2][2][2] = {{5,6}, {7,8}, {9,10}, {11,12}};
      |           |      |      |       |
      |           |      |      |       |
      -----------------------------------

, 6.7.9/2:

, .

, , :

int a[][2][2] = {{5,6}, {7,8}, {9,10}, {11,12}};

, :

int a[4][2][2] = {
    {
        {5, 6},
        {0, 0}
    },
    {
        {7, 8},
        {0, 0}
    },
    {
        {9, 10},
        {0, 0}
    },
    {
        {11, 12},
        {0, 0}
    }  
};

, :

int a[2][2][2] = {5, 6, 7, 8, 9, 10, 11, 12}; // legal, but not advisable

:

int a[2][2][2] = {{5, 6, 7, 8}, {9, 10, 11, 12}}; // bad style

6.7.9/20:

, ; , subaggregate contains union .

+1

All Articles