如何确定C语言中数组的大小?
也就是说,数组可以容纳多少元素?
如何确定C语言中数组的大小?
也就是说,数组可以容纳多少元素?
当前回答
要知道在代码中显式声明并由其变量引用的固定数组的大小,可以使用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);
}
其他回答
如果你真的想这样做来传递你的数组,我建议实现一个结构来存储你想要数组类型的指针和一个表示数组大小的整数。然后你可以把它传递给你的函数。只需将数组变量值(指向第一个元素的指针)赋给该指针即可。然后你可以选择数组。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;
}
大小的“技巧”是我所知道的最好的方法,在括号的使用上有一个小而重要的变化(对我来说,这是一个主要的烦恼)。
正如维基百科的条目所表明的那样,C语言的sizeof不是一个函数;它是一个算子。因此,除非实参是类型名,否则不需要在实参周围加上圆括号。这很容易记住,因为它使实参看起来像一个强制转换表达式,它也使用括号。
所以:如果你有以下情况:
int myArray[10];
你可以用这样的代码找到元素的数量:
size_t n = sizeof myArray / sizeof *myArray;
对我来说,这比用括号代替要容易得多。我还赞成在除法的右边部分使用星号,因为它比索引更简洁。
当然,这也都是编译时的,所以没有必要担心除法会影响程序的性能。所以尽可能使用这个形式。
当你有一个实际对象时,使用sizeof总是最好的,而不是在一个类型上,因为这样你就不需要担心犯错误并声明错误的类型。
例如,假设您有一个函数,它将一些数据输出为字节流,例如跨网络输出。让我们调用send()函数,并将一个指向要发送的对象的指针和该对象中的字节数作为参数。所以,原型变成:
void send(const void *object, size_t size);
然后你需要发送一个整数,所以你像这样编码:
int foo = 4711;
send(&foo, sizeof (int));
现在,通过在两个地方指定foo类型,您引入了一种搬起石头砸自己的脚的微妙方法。如果一个改变了,而另一个没有改变,代码就会崩溃。因此,总是这样做:
send(&foo, sizeof foo);
现在你被保护了。当然,你复制了变量的名称,但如果你改变了它,编译器很可能会检测到这种破坏。
你可以使用&操作符。以下是源代码:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10];
int *p;
printf("%p\n", (void *)a);
printf("%p\n", (void *)(&a+1));
printf("---- diff----\n");
printf("%zu\n", sizeof(a[0]));
printf("The size of array a is %zu\n", ((char *)(&a+1)-(char *)a)/(sizeof(a[0])));
return 0;
};
下面是示例输出
1549216672
1549216712
---- diff----
4
The size of array a is 10
对于预定义数组:
int a[] = {1, 2, 3, 4, 5, 6};
计算数组中的元素数量:
element _count = sizeof(a) / sizeof(a[0]);
每个人都在使用的宏ARRAYELEMENTCOUNT(x)的计算结果不正确。实际上,这只是一个敏感的问题,因为你不能有导致“数组”类型的表达式。
/* Compile as: CL /P "macro.c" */
# define ARRAYELEMENTCOUNT(x) (sizeof (x) / sizeof (x[0]))
ARRAYELEMENTCOUNT(p + 1);
实际计算结果为:
(sizeof (p + 1) / sizeof (p + 1[0]));
而
/* Compile as: CL /P "macro.c" */
# define ARRAYELEMENTCOUNT(x) (sizeof (x) / sizeof (x)[0])
ARRAYELEMENTCOUNT(p + 1);
它正确地计算为:
(sizeof (p + 1) / sizeof (p + 1)[0]);
这和数组的大小没有太大关系。我刚刚注意到很多错误,因为没有真正观察C预处理器是如何工作的。您总是包装宏参数,而不是可能涉及的表达式。
这是正确的;我的例子很糟糕。但这正是应该发生的。正如我前面提到的,p + 1将以指针类型结束,并使整个宏无效(就像您试图在带有指针形参的函数中使用宏一样)。
在一天结束的时候,在这个特殊的例子中,错误并不重要(所以我只是在浪费大家的时间;Huzzah !),因为你没有数组类型的表达式。但是我认为关于预处理器评估的微妙之处是很重要的。