如何获得数组大小

时间:2022-01-22 21:28:20

I'd like to know how to get an array rows & columns size. For instance it would be something like this:

我想知道如何获得数组行和列大小。例如,它将是这样的:

int matrix[][] = { { 2, 3 , 4}, { 1, 5, 3 } }

The size of this one would be 2 x 3. How can I calculate this without including other libraries but stdio or stdlib?

这个的大小是2 x 3.如何在不包括其他库但stdio或stdlib的情况下计算出来?

4 个解决方案

#1


10  

This has some fairly limited use, but it's possible to do so with sizeof.

这有一些相当有限的用途,但有可能使用sizeof。

sizeof(matrix) = 24  // 2 * 3 ints (each int is sizeof 4)
sizeof(matrix[0]) = 12  // 3 ints
sizeof(matrix[0][0]) = 4  // 1 int

So,

int num_rows = sizeof(matrix) / sizeof(matrix[0]);
int num_cols = sizeof(matrix[0]) / sizeof(matrix[0][0]);

Or define your own macro:

或者定义自己的宏:

#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))

int num_rows = ARRAYSIZE(matrix);
int num_cols = ARRAYSIZE(matrix[0]);

Or even:

#define NUM_ROWS(a) ARRAYSIZE(a)
int num_rows = NUM_ROWS(matrix);
#define NUM_COLS(a) ARRAYSIZE(a[0])
int num_cols = NUM_COLS(matrix);

But, be aware that you can't pass around this int[][] matrix and then use the macro trick. Unfortunately, unlike higher level languages (java, python), this extra information isn't passed around with the array (you would need a struct, or a class in c++). The matrix variable simply points to a block of memory where the matrix lives.

但是,请注意,您无法传递此int [] []矩阵,然后使用宏技巧。不幸的是,与更高级别的语言(java,python)不同,这些额外的信息不会与数组一起传递(你需要一个结构或c ++中的类)。矩阵变量只是指向矩阵所在的内存块。

#2


5  

It cannot be "something like this". The syntax that involves two (or more) consecutive empty square brackets [][] is never valid in C language.

它不可能是“像这样的东西”。涉及两个(或更多)连续空方括号[] []的语法在C语言中永远不会有效。

When you are declaring an array with an initializer, only the first size can be omitted. All remaining sizes must be explicitly present. So in your case the second size can be 3

当您使用初始化程序声明数组时,只能省略第一个大小。必须明确显示所有剩余尺寸。所以在你的情况下,第二个大小可以是3

int matrix[][3] = { { 2, 3, 4 }, { 1, 5, 3 } };

if you intended it to be 3. Or it can be 5, if you so desire

如果你打算成为3.如果你愿意,它可以是5

int matrix[][5] = { { 2, 3, 4 }, { 1, 5, 3 } };

In other words, you will always already "know" all sizes except the first. There's no way around it. Yet, if you want to "recall" it somewhere down the code, you can obtain it as

换句话说,除了第一个,你将始终“知道”所有尺寸。没有办法绕过它。然而,如果你想在代码的某处“召回”它,你可以把它作为代码

sizeof *matrix / sizeof **matrix

As for the first size, you can get it as

至于第一个尺寸,你可以得到它

sizeof matrix / sizeof *matrix

#3


0  

sizeof (matrix) will give you the total size of the array, in bytes. sizeof (matrix[0]) / sizeof (matrix[0][0]) gives you the dimension of the inner array, and sizeof (matrix) / sizeof (matrix[0]) should be the outer dimension.

sizeof(矩阵)将为您提供数组的总大小(以字节为单位)。 sizeof(matrix [0])/ sizeof(matrix [0] [0])为您提供内部数组的维度,sizeof(matrix)/ sizeof(matrix [0])应该是外部维度。

#4


0  

Example for size of :

尺寸示例:

#include <stdio.h>
#include <conio.h>
int array[6]= { 1, 2, 3, 4, 5, 6 };
void main() {
  clrscr();
  int len=sizeof(array)/sizeof(int);
  printf("Length Of Array=%d", len);
  getch();
}

#1


10  

This has some fairly limited use, but it's possible to do so with sizeof.

这有一些相当有限的用途,但有可能使用sizeof。

sizeof(matrix) = 24  // 2 * 3 ints (each int is sizeof 4)
sizeof(matrix[0]) = 12  // 3 ints
sizeof(matrix[0][0]) = 4  // 1 int

So,

int num_rows = sizeof(matrix) / sizeof(matrix[0]);
int num_cols = sizeof(matrix[0]) / sizeof(matrix[0][0]);

Or define your own macro:

或者定义自己的宏:

#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))

int num_rows = ARRAYSIZE(matrix);
int num_cols = ARRAYSIZE(matrix[0]);

Or even:

#define NUM_ROWS(a) ARRAYSIZE(a)
int num_rows = NUM_ROWS(matrix);
#define NUM_COLS(a) ARRAYSIZE(a[0])
int num_cols = NUM_COLS(matrix);

But, be aware that you can't pass around this int[][] matrix and then use the macro trick. Unfortunately, unlike higher level languages (java, python), this extra information isn't passed around with the array (you would need a struct, or a class in c++). The matrix variable simply points to a block of memory where the matrix lives.

但是,请注意,您无法传递此int [] []矩阵,然后使用宏技巧。不幸的是,与更高级别的语言(java,python)不同,这些额外的信息不会与数组一起传递(你需要一个结构或c ++中的类)。矩阵变量只是指向矩阵所在的内存块。

#2


5  

It cannot be "something like this". The syntax that involves two (or more) consecutive empty square brackets [][] is never valid in C language.

它不可能是“像这样的东西”。涉及两个(或更多)连续空方括号[] []的语法在C语言中永远不会有效。

When you are declaring an array with an initializer, only the first size can be omitted. All remaining sizes must be explicitly present. So in your case the second size can be 3

当您使用初始化程序声明数组时,只能省略第一个大小。必须明确显示所有剩余尺寸。所以在你的情况下,第二个大小可以是3

int matrix[][3] = { { 2, 3, 4 }, { 1, 5, 3 } };

if you intended it to be 3. Or it can be 5, if you so desire

如果你打算成为3.如果你愿意,它可以是5

int matrix[][5] = { { 2, 3, 4 }, { 1, 5, 3 } };

In other words, you will always already "know" all sizes except the first. There's no way around it. Yet, if you want to "recall" it somewhere down the code, you can obtain it as

换句话说,除了第一个,你将始终“知道”所有尺寸。没有办法绕过它。然而,如果你想在代码的某处“召回”它,你可以把它作为代码

sizeof *matrix / sizeof **matrix

As for the first size, you can get it as

至于第一个尺寸,你可以得到它

sizeof matrix / sizeof *matrix

#3


0  

sizeof (matrix) will give you the total size of the array, in bytes. sizeof (matrix[0]) / sizeof (matrix[0][0]) gives you the dimension of the inner array, and sizeof (matrix) / sizeof (matrix[0]) should be the outer dimension.

sizeof(矩阵)将为您提供数组的总大小(以字节为单位)。 sizeof(matrix [0])/ sizeof(matrix [0] [0])为您提供内部数组的维度,sizeof(matrix)/ sizeof(matrix [0])应该是外部维度。

#4


0  

Example for size of :

尺寸示例:

#include <stdio.h>
#include <conio.h>
int array[6]= { 1, 2, 3, 4, 5, 6 };
void main() {
  clrscr();
  int len=sizeof(array)/sizeof(int);
  printf("Length Of Array=%d", len);
  getch();
}