Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Size of two dimensional array

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

#include <stdio.h>

int main() {

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[0])/row;

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

}

Output:
Number of rows: 10
Number of columns: 0

like image 241
Wickerman Avatar asked Dec 07 '15 12:12

Wickerman


People also ask

What is the size of 2D array in C?

Here i and j are the size of the two dimensions, i.e I denote the number of rows while j denotes the number of columns. Example: int A[10][20]; Here we declare a two-dimensional array in C, named A which has 10 rows and 20 columns.

How do you find the size of a two-dimensional array?

We use arrayname. length to determine the number of rows in a 2D array because the length of a 2D array is equal to the number of rows it has. The number of columns may vary row to row, which is why the number of rows is used as the length of the 2D array.

How big can a 2D array be C++?

codeblocks and dev c++ can take upto (10,00,000) array globally(by this I mean outside any function).

What is the dimension of C array in?

The array can hold 12 elements. You can think the array as a table with 3 rows and each row has 4 columns. Similarly, you can declare a three-dimensional (3d) array.


Video Answer


3 Answers

That's a problem of integer division!

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

should be

int column = 7 / 10;

and in integer division, 7/10==0.

What you want to do is divide the length of one row, eg. sizeof(result[0]) by the size of one element of that row, eg. sizeof(result[0][0]):

int column = sizeof(result[0])/sizeof(result[0][0]);
like image 56
Marcus Müller Avatar answered Oct 27 '22 09:10

Marcus Müller


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

#include <stdio.h>

#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;
}
like image 21
August Karlstrom Avatar answered Oct 27 '22 08:10

August Karlstrom


This works for me (comments explains why):

#include <stdio.h>

int main() {

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

   }; 

   // 'total' will be 70 = 10 * 7
   int total = sizeof(result);

   // 'column' will be 7 = size of first row
   int column = sizeof(result[0]);

   // 'row' will be 10 = 70 / 7
   int row = total / column;

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

}

And the output of this is:

Total of fields: 70
Number of rows: 10
Number of columns: 7

EDIT:

As pointed by @AnorZaken, passing the array to a function as a parameter and printing the result of sizeof on it, will output another total. This is because when you pass an array as argument (not a pointer to it), C will pass it as copy and will apply some C magic in between, so you are not passing exactly the same as you think you are. To be sure about what you are doing and to avoid some extra CPU work and memory consumption, it's better to pass arrays and objects by reference (using pointers). So you can use something like this, with same results as original:

#include <stdio.h>

void foo(char (*result)[10][7])
{
   // 'total' will be 70 = 10 * 7
   int total = sizeof(*result);

   // 'column' will be 7 = size of first row
   int column = sizeof((*result)[0]);

   // 'row' will be 10 = 70 / 7
   int row = total / column;

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

}

int main(void) {

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

   };

   foo(&result);

   return 0;
}
like image 38
emi Avatar answered Oct 27 '22 10:10

emi