C: Size of two dimensional array

后端 未结 5 1866
无人共我
无人共我 2021-02-12 09:37

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:25

    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;
    }
    

提交回复
热议问题