How to get the rows and columns of a matrix (2D array) in C

2 Answers

0 votes
#include <stdio.h>  
       
int main()  
{  
    int arr[][4] = {     
                    {1, 2, 3, 5},  
                    {4, 5, 6, 5},  
                    {7, 8, 9, 5}  
                };  

    int rows = (sizeof(arr) / sizeof(arr[0]));  
    int cols = (sizeof(arr) / sizeof(arr[0][0])) / rows;  
          
    printf("rows = %d\n", rows);  
    printf("cols = %d\n", cols);  
 
    return 0;  
}  




/*

run:

rows = 3
cols = 4

*/

 



answered Feb 20, 2021 by avibootz
edited Feb 22, 2022 by avibootz
0 votes
#include <stdio.h>  
       
int main()  
{  
    int arr[][4] = {     
                    {1, 2, 3, 5},  
                    {4, 5, 6, 5},  
                    {7, 8, 9, 5}  
                };  

    int rows = sizeof(arr) / sizeof(arr[0]);
    int cols = sizeof(arr[0]) / sizeof(arr[0][0]);
          
    printf("rows = %d\n", rows);  
    printf("cols = %d\n", cols);  
 
    return 0;  
}  




/*

run:

rows = 3
cols = 4

*/

 



answered Feb 20, 2021 by avibootz

Related questions

2 answers 232 views
1 answer 227 views
1 answer 252 views
1 answer 189 views
1 answer 183 views
...