C: Size of two dimensional array

后端 未结 5 867
余生分开走
余生分开走 2021-02-12 09:23

I need some help counting the rows and columns of a two dimensional array. It seems like I can\'t count columns?

#include 

int main() {

char res         


        
5条回答
  •  醉酒成梦
    2021-02-12 10:18

        // gets you the total size of the 2d array 
        printf("Arrays Total size: %ld\n",sizeof(result));
    
        // gets you the cumulative size of row which is 5 columns * sizeof(int)
        printf("1 row cumulative size: %ld\n",sizeof(result[0]));
    
        // division of total array size with cumulative size of row gets you total number of rows
        printf("total number of rows: %ld\n",sizeof(result)/sizeof(result[0]));
    
        // and total number of columns you get by dividing cumulative row size with sizeof(char)
        printf("total number of columns: %ld\n",sizeof(result[0])/sizeof(char));
    

提交回复
热议问题