How to print the array?

前端 未结 5 553
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  Happy的楠姐
    2021-02-02 07:50

    It looks like you have a typo on your array, it should read:

    int my_array[3][3] = {...
    

    You don't have the _ or the {.

    Also my_array[3][3] is an invalid location. Since computers begin counting at 0, you are accessing position 4. (Arrays are weird like that).

    If you want just the last element:

    printf("%d\n", my_array[2][2]);
    

    If you want the entire array:

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

提交回复
热议问题