如何确定C语言中数组的大小?
也就是说,数组可以容纳多少元素?
如何确定C语言中数组的大小?
也就是说,数组可以容纳多少元素?
当前回答
如果你真的想这样做来传递你的数组,我建议实现一个结构来存储你想要数组类型的指针和一个表示数组大小的整数。然后你可以把它传递给你的函数。只需将数组变量值(指向第一个元素的指针)赋给该指针即可。然后你可以选择数组。arr[i]获取第i个元素并使用Array。Size来获取数组中元素的数量。
我为您提供了一些代码。它不是很有用,但是你可以用更多的特性来扩展它。老实说,如果这些是你想要的东西,你应该停止使用C语言,使用另一种内置这些功能的语言。
/* Absolutely no one should use this...
By the time you're done implementing it you'll wish you just passed around
an array and size to your functions */
/* This is a static implementation. You can get a dynamic implementation and
cut out the array in main by using the stdlib memory allocation methods,
but it will work much slower since it will store your array on the heap */
#include <stdio.h>
#include <string.h>
/*
#include "MyTypeArray.h"
*/
/* MyTypeArray.h
#ifndef MYTYPE_ARRAY
#define MYTYPE_ARRAY
*/
typedef struct MyType
{
int age;
char name[20];
} MyType;
typedef struct MyTypeArray
{
int size;
MyType *arr;
} MyTypeArray;
MyType new_MyType(int age, char *name);
MyTypeArray newMyTypeArray(int size, MyType *first);
/*
#endif
End MyTypeArray.h */
/* MyTypeArray.c */
MyType new_MyType(int age, char *name)
{
MyType d;
d.age = age;
strcpy(d.name, name);
return d;
}
MyTypeArray new_MyTypeArray(int size, MyType *first)
{
MyTypeArray d;
d.size = size;
d.arr = first;
return d;
}
/* End MyTypeArray.c */
void print_MyType_names(MyTypeArray d)
{
int i;
for (i = 0; i < d.size; i++)
{
printf("Name: %s, Age: %d\n", d.arr[i].name, d.arr[i].age);
}
}
int main()
{
/* First create an array on the stack to store our elements in.
Note we could create an empty array with a size instead and
set the elements later. */
MyType arr[] = {new_MyType(10, "Sam"), new_MyType(3, "Baxter")};
/* Now create a "MyTypeArray" which will use the array we just
created internally. Really it will just store the value of the pointer
"arr". Here we are manually setting the size. You can use the sizeof
trick here instead if you're sure it will work with your compiler. */
MyTypeArray array = new_MyTypeArray(2, arr);
/* MyTypeArray array = new_MyTypeArray(sizeof(arr)/sizeof(arr[0]), arr); */
print_MyType_names(array);
return 0;
}
其他回答
执行概要:
int a[17];
size_t n = sizeof(a)/sizeof(a[0]);
完整的回答:
要确定数组的字节大小,可以使用sizeof 接线员:
int a[17];
size_t n = sizeof(a);
在我的电脑上,int是4字节长,所以n是68。
要确定数组中元素的数量,我们可以除法 数组的总大小乘以数组元素的大小。 你可以这样处理类型,像这样:
int a[17];
size_t n = sizeof(a) / sizeof(int);
并得到正确的答案(68 / 4 = 17)但如果类型 如果你忘记改变,你会有一个讨厌的bug sizeof(int)也是。
因此首选除数是sizeof(a[0])或等效的sizeof(*a),即数组第一个元素的大小。
int a[17];
size_t n = sizeof(a) / sizeof(a[0]);
另一个优点是您现在可以轻松地参数化 宏中的数组名,get:
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
int a[17];
size_t n = NELEMS(a);
如果你知道数组的数据类型,你可以像这样使用:
int arr[] = {23, 12, 423, 43, 21, 43, 65, 76, 22};
int noofele = sizeof(arr)/sizeof(int);
或者如果你不知道数组的数据类型,你可以使用类似这样的东西:
noofele = sizeof(arr)/sizeof(arr[0]);
注意:只有当数组在运行时没有定义(如malloc)并且数组没有传递到函数中时,这个东西才有效。在这两种情况下,arr(数组名)都是指针。
要知道在代码中显式声明并由其变量引用的固定数组的大小,可以使用sizeof,例如:
int a[10];
int len = sizeof(a)/sizeof(int);
但这通常是没有用的,因为你已经知道答案了。
但如果你有一个指针,你不能使用sizeof,这是一个原则问题。
但是…由于数组是作为线性内存提供给用户的,如果您知道最后一个元素地址,并且如果您知道类型的大小,那么您就可以计算它有多少个元素。例如:
#include <stdio.h>
int main(){
int a[10];
printf("%d\n", sizeof(a)/sizeof(int));
int *first = a;
int *last = &(a[9]);
printf("%d\n", (last-first) + 1);
}
输出:
10
10
另外,如果你不能利用编译时,你可以:
#include <stdio.h>
int main(){
int a[10];
printf("%d\n", sizeof(a)/sizeof(int));
void *first = a;
void *last = &(a[9]);
printf("%d\n", (last-first)/sizeof(int) + 1);
}
一个更优雅的解决方案是
size_t size = sizeof(a) / sizeof(*a);
sizeof函数返回数组在内存中使用的字节数。如果你想计算数组中的元素数量,你应该用数组的sizeof变量类型除以这个数字。让我们说int array[10];,如果变量类型integer在你的计算机是32位(或4字节),为了得到你的数组的大小,你应该做以下工作:
int array[10];
size_t sizeOfArray = sizeof(array)/sizeof(int);