维c粉的作用及功效:C:二维数组的大小(c double array size)

我需要一些帮助来计算二维数组的行和列。

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

输出:
行数:10
列数:0

19

这是一个整数除法的问题!

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

应该是

int column = 7 / 10;

在整数除法中,7/10==0

你想要做的是将一行的长度,例如sizeof(result[0])除以该行的一个元素的大小,例如sizeof(result[0][0])

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

使用数组长度宏更方便(并且更不容易出错):

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

这对我有用(注释解释了为什么):

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

这个的输出是:

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

正如 @ AnorZaken 所指出的,将数组作为参数传递给一个函数,并在其上打印sizeof的结果,将输出另一个total。这是因为当您将数组作为参数传递 (而不是指向它的指针) 时,C 将其作为副本传递,并且会在两者之间应用一些 C 魔术,因此您不会像您认为的那样传递相同的原始数组和额外的

#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;
}
0
    // gets you the total size of the 2d array 
    printf("Arrays Total size: %ld\n",sizeof(result));
    // gets you the cumulative size of row which is 5 columns * sizeof(int)
    printf("1 row cumulative size: %ld\n",sizeof(result[0]));
    // division of total array size with cumulative size of row gets you total number of rows
    printf("total number of rows: %ld\n",sizeof(result)/sizeof(result[0]));
    // and total number of columns you get by dividing cumulative row size with sizeof(char)
    printf("total number of columns: %ld\n",sizeof(result[0])/sizeof(char));

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(24)
以编程方式查找计算机上的内核数
上一篇
服务器和云计算的关系:云计算和无服务器计算有什么区别
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(81条)