Home > Enterprise >  How to find number of rows and columns in a 2D array in C (any simpler way than this?)
How to find number of rows and columns in a 2D array in C (any simpler way than this?)

Time:01-19

I have this code fragment:

    char result[10][7] = {
        {'1', 'X', '2', 'X', '2', '1', '1'},
        {'X', '1', '1', '2', '2', '1', '1'},
        {'X', '1', '1', '2', '2', '1', '1'},
        {'1', 'X', '2', 'X', '2', '2', '2'},
        {'1', 'X', '1', 'X', '1', 'X', '2'},
        {'1', 'X', '2', 'X', '2', '1', '1'},
        {'1', 'X', '2', '2', '1', 'X', '1'},
        {'1', 'X', '2', 'X', '2', '1', 'X'},
        {'1', '1', '1', 'X', '2', '2', '1'},
        {'1', 'X', '2', 'X', '2', '1', '1'}

    };

    int row = sizeof(result) / sizeof(result[0]);
    int column = sizeof(result) / row;

    printf("Number of rows: %d\n", row);
    printf("Number of columns: %d\n", column);

is there any prebuilt function or anything like that especially for calculating number of columns? I am asking this because we get the number of total elements as sizeof(arr) / sizeof(int) and need another division, but I want to compute the number of columns directly.

CodePudding user response:

To compute the number of elements of any defined array, you can use the sizeof operator regardless of the array element type:

some_type A[] = { ... };
size_t length = sizeof(A) / sizeof(A[0]);

For your 2D array definition, you can get the number of columns as the length of array result[0].

Here is a modified version of your code to get the lengths of both dimensions directly:

#include <stdio.h>

int main() {
    char result[][7] = {
        {'1', 'X', '2', 'X', '2', '1', '1'},
        {'X', '1', '1', '2', '2', '1', '1'},
        {'X', '1', '1', '2', '2', '1', '1'},
        {'1', 'X', '2', 'X', '2', '2', '2'},
        {'1', 'X', '1', 'X', '1', 'X', '2'},
        {'1', 'X', '2', 'X', '2', '1', '1'},
        {'1', 'X', '2', '2', '1', 'X', '1'},
        {'1', 'X', '2', 'X', '2', '1', 'X'},
        {'1', '1', '1', 'X', '2', '2', '1'},
        {'1', 'X', '2', 'X', '2', '1', '1'},
    };

    size_t rows = sizeof(result) / sizeof(result[0]);
    size_t cols = sizeof(result[0]) / sizeof(result[0][0]);

    printf("Number of rows: %zu\n", rows);
    printf("Number of columns: %zu\n", cols);
    return 0;
}

Note however that the expressions for row and column in your code are computed at compile time by optimizing compilers.

CodePudding user response:

There is no built-in function specifically for calculating the number of columns in a two-dimensional array in C. The way it is typically done is by using the sizeof operator to determine the size of the array and then dividing it by the size of one of its rows or one of its elements.

As you pointed out, in your code snippet, you have calculated the number of rows correctly by dividing the size of the entire array by the size of one row. To calculate the number of columns, you can use the same method by dividing the size of the entire array by the size of one element, which in this case is a char.

In your case, you have already defined the number of columns, so you can just use it instead of calculating it.

int column = sizeof(result[0]) / sizeof(char);

It should be noted that if your array is not a rectangular array, meaning the number of columns is not the same for all rows, then you would need to calculate the number of columns for each row individually and not using the sizeof operator.

  •  Tags:  
  • c
  • Related