如何获取数组中的元素数?

时间:2021-02-12 03:38:35

I created an array and put the value into array as follow

我创建了一个数组并将值放入数组中,如下所示

int *ptr_int;
int list_int[10];
int i;
for (i=0; i<10; i++)
    list_int[i] = i + 1;

and I assign a value into list_int array like this

我像这样在list_int数组中赋值

list_int[17] = 18;

When I tried to get the count of array as follow

当我试图得到数组的数量如下

int size = sizeof(list_int ) / sizeof( int );
printf( "The size of int is %d.\n", size);

the result is only 10.

结果只有10个。

How could I get the array room count?

我怎么能得到阵列室数?

4 个解决方案

#1


6  

the result is only 10.

结果只有10个。

That's the real size. Assigning to list_int[17] is undefined behavior, and it does not magically extend the array.

这是真正的大小。分配给list_int [17]是未定义的行为,它不会神奇地扩展数组。

#2


1  

You have already defined the max size of your array as 10 with the following definition

您已使用以下定义将阵列的最大大小定义为10

int list_int[10];

You can not assign value to list_int[17]. Becacuse list_int[17] is out of the memory of the array list_int[10] that you have defined.

您无法为list_int分配值[17]。 Becacuse list_int [17]超出了你定义的数组list_int [10]的内存。

You can only assign value to the element from 0 .. 9

您只能为0到9中的元素赋值

#3


1  

list_int[17] = 18;

This is undefined behavior because the array size is only 10.

这是未定义的行为,因为数组大小仅为10。

Note that with the exception of variable length arrays, sizeof operator is a compile time operator. The result of sizeof(list_int ) and sizeof(int) are determined in compile time.

请注意,除了可变长度数组之外,sizeof运算符是编译时运算符。 sizeof(list_int)和sizeof(int)的结果在编译时确定。


To implement an array with dynamic size, you need to allocate the array dynamically, you may find realloc pretty helpful.

要实现具有动态大小的数组,您需要动态分配数组,您可能会发现realloc非常有用。

#4


1  

To Create Dynamic arrays ( allocated at run time )

创建动态数组(在运行时分配)

int n; 

scanf("%d",&n); // read n from the user at run time 

int* x=(int*)malloc(sizeof(int)*n);  // where n is the number of elements you need to allocate

////// after that you can access the array (x) using indexer 

///// reading loop 
for(int i=0;i<n;i++)
scanf("%d",&x[i]);

===============================================================================

================================================== =============================

Note: if you need more dynamic data structure which can allocate memory for each entry you can use linked lists

注意:如果您需要更多动态数据结构,可以为每个条目分配内存,则可以使用链接列表

#1


6  

the result is only 10.

结果只有10个。

That's the real size. Assigning to list_int[17] is undefined behavior, and it does not magically extend the array.

这是真正的大小。分配给list_int [17]是未定义的行为,它不会神奇地扩展数组。

#2


1  

You have already defined the max size of your array as 10 with the following definition

您已使用以下定义将阵列的最大大小定义为10

int list_int[10];

You can not assign value to list_int[17]. Becacuse list_int[17] is out of the memory of the array list_int[10] that you have defined.

您无法为list_int分配值[17]。 Becacuse list_int [17]超出了你定义的数组list_int [10]的内存。

You can only assign value to the element from 0 .. 9

您只能为0到9中的元素赋值

#3


1  

list_int[17] = 18;

This is undefined behavior because the array size is only 10.

这是未定义的行为,因为数组大小仅为10。

Note that with the exception of variable length arrays, sizeof operator is a compile time operator. The result of sizeof(list_int ) and sizeof(int) are determined in compile time.

请注意,除了可变长度数组之外,sizeof运算符是编译时运算符。 sizeof(list_int)和sizeof(int)的结果在编译时确定。


To implement an array with dynamic size, you need to allocate the array dynamically, you may find realloc pretty helpful.

要实现具有动态大小的数组,您需要动态分配数组,您可能会发现realloc非常有用。

#4


1  

To Create Dynamic arrays ( allocated at run time )

创建动态数组(在运行时分配)

int n; 

scanf("%d",&n); // read n from the user at run time 

int* x=(int*)malloc(sizeof(int)*n);  // where n is the number of elements you need to allocate

////// after that you can access the array (x) using indexer 

///// reading loop 
for(int i=0;i<n;i++)
scanf("%d",&x[i]);

===============================================================================

================================================== =============================

Note: if you need more dynamic data structure which can allocate memory for each entry you can use linked lists

注意:如果您需要更多动态数据结构,可以为每个条目分配内存,则可以使用链接列表