How to initialize N dimensional arrays in C without using loop

后端 未结 4 899
夕颜
夕颜 2021-01-25 07:00

I want to initalize a 3 x 3 matrix with first two rows as 0\'s and last row as 1\'s. I have declared a 2D array int matrix[3][3]

I want to initialize it wit

4条回答
  •  感动是毒
    2021-01-25 07:30

    int matrix[3][3] = {
        { 0, 0, 0 },
        { 0, 0, 0 },
        { 1, 1, 1 }
    };
    

    Or, the more compact:

    int matrix[3][3] = {
        [2] = { 1, 1, 1 }
    };
    

    The solution generalizes for N so long as N is fixed. If N is large, you can use mouviciel's answer to this question.

提交回复
热议问题