How to print the array?

前端 未结 5 538
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-02 07:06
int main() {  
  int my array[3][3] =
    10, 23, 42,    
    1, 654, 0,  
    40652, 22, 0  
  };  

  printf(\"%d\\n\", my_array[3][3]);  
  return 0;
}
5条回答
  •  轮回少年
    2021-02-02 07:38

    What you are doing is printing the value in the array at spot [3][3], which is invalid for a 3by3 array, you need to loop over all the spots and print them.

    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
            printf("%d ", array[i][j]);
        }
        printf("\n");
    } 
    

    This will print it in the following format

    10 23 42
    1 654 0
    40652 22 0
    

    if you want more exact formatting you'll have to change how the printf is formatted.

提交回复
热议问题