how to find 2d array size in c++

后端 未结 9 825
余生分开走
余生分开走 2020-12-23 12:14

How do I find the size of a 2D array in C++? Is there any predefined function like sizeof to determine the size of the array?

Also, can anyone tell me h

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 12:57

    Suppose you were only allowed to use array then you could find the size of 2-d array by the following way.

      int ary[][5] = { {1, 2, 3, 4, 5},
                       {6, 7, 8, 9, 0}
                     };
    
      int rows =  sizeof ary / sizeof ary[0]; // 2 rows  
    
      int cols = sizeof ary[0] / sizeof(int); // 5 cols
    

提交回复
热议问题