C: Size of two dimensional array

后端 未结 5 863
余生分开走
余生分开走 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:17

    It's much more convenient (and less error prone) to use an array length macro:

    #include 
    
    #define LEN(arr) ((int) (sizeof (arr) / sizeof (arr)[0]))
    
    int main(void)
    {
        char result[10][7];
    
        printf("Number of rows: %d\n", LEN(result));
        printf("Number of columns: %d\n", LEN(result[0]));
        return 0;
    }
    

提交回复
热议问题