C:二维阵列的大小

时间:2022-11-20 21:34:13

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

输出:行数:10列数:0

3 个解决方案

#1


4  

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.

在整数除法中,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]):

你想要做的是划分一行的长度,例如。 sizeof(result [0])乘以该行的一个元素的大小,例如。的sizeof(结果[0] [0]):

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

#2


7  

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

#3


5  

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:

正如@AnorZaken指出的那样,将数组作为参数传递给函数并在其上打印sizeof的结果,将输出另一个总数。这是因为当你传递一个数组作为参数(而不是指向它的指针)时,C会将它作为副本传递,并在它们之间应用一些C魔法,所以你没有像你想象的那样传递完全相同的东西。为了确定你正在做什么并避免一些额外的CPU工作和内存消耗,最好通过引用传递数组和对象(使用指针)。所以你可以使用这样的东西,结果和原作相同:

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

#1


4  

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.

在整数除法中,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]):

你想要做的是划分一行的长度,例如。 sizeof(result [0])乘以该行的一个元素的大小,例如。的sizeof(结果[0] [0]):

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

#2


7  

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

#3


5  

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:

正如@AnorZaken指出的那样,将数组作为参数传递给函数并在其上打印sizeof的结果,将输出另一个总数。这是因为当你传递一个数组作为参数(而不是指向它的指针)时,C会将它作为副本传递,并在它们之间应用一些C魔法,所以你没有像你想象的那样传递完全相同的东西。为了确定你正在做什么并避免一些额外的CPU工作和内存消耗,最好通过引用传递数组和对象(使用指针)。所以你可以使用这样的东西,结果和原作相同:

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