How to initialize N dimensional arrays in C without using loop

后端 未结 4 863
夕颜
夕颜 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:33

    matrix[0][2] = matrix[0][1] = matrix[0][0] =
    matrix[1][2] = matrix[1][1] = matrix[1][0] = 0;
    matrix[2][2] = matrix[2][1] = matrix[2][0] = 1;
    

    or

    #include 
    ...
    memset(matrix, 0, sizeof(matrix));
    matrix[2][2] = matrix[2][1] = matrix[2][0] = 1;
    

提交回复
热议问题