C 传递数组的大小
可能的重复:
如何查找指向的指针的 sizeof(一个数组)
我知道 sizeof 运算符在编译时被评估并替换为常量。鉴于此,在程序中的不同点传递不同数组的函数如何计算其大小?我可以将它作为参数传递给函数,但如果不是绝对必要,我宁愿不必添加另一个参数。
这是一个例子来说明我的要求:
#include <stdio.h>
#include <stdlib.h>
#define SIZEOF(a) ( sizeof a / sizeof a[0] )
void printarray( double x[], int );
int main()
{
double array1[ 100 ];
printf( "The size of array1 = %ld.\n", SIZEOF( array1 ));
printf( "The size of array1 = %ld.\n", sizeof array1 );
printf( "The size of array1[0] = %ld.\n\n", sizeof array1[0] );
printarray( array1, SIZEOF( array1 ) );
return EXIT_SUCCESS;
}
void printarray( double p[], int s )
{
int i;
// THIS IS WHAT DOESN"T WORK, SO AS A CONSEQUENCE, I PASS THE
// SIZE IN AS A SECOND PARAMETER, WHICH I'D RATHER NOT DO.
printf( "The size of p calculated = %ld.\n", SIZEOF( p ));
printf( "The size of p = %ld.\n", sizeof p );
printf( "The size of p[0] = %ld.\n", sizeof p[0] );
for( i = 0; i < s; i++ )
printf( "Eelement %d = %lf.\n", i, p[i] );
return;
}
Possible Duplicate:
How to find the sizeof( a pointer pointing to an array )
I understand that the sizeof operator is evaluated and replaced with a constant at compile time. Given that, how can a function, being passed different arrays at different points in a program, have it's size computed? I can pass it as a parameter to the function, but I'd rather not have to add another parameter if I don't absolutely have to.
Here's an example to illustrate what I'm asking:
#include <stdio.h>
#include <stdlib.h>
#define SIZEOF(a) ( sizeof a / sizeof a[0] )
void printarray( double x[], int );
int main()
{
double array1[ 100 ];
printf( "The size of array1 = %ld.\n", SIZEOF( array1 ));
printf( "The size of array1 = %ld.\n", sizeof array1 );
printf( "The size of array1[0] = %ld.\n\n", sizeof array1[0] );
printarray( array1, SIZEOF( array1 ) );
return EXIT_SUCCESS;
}
void printarray( double p[], int s )
{
int i;
// THIS IS WHAT DOESN"T WORK, SO AS A CONSEQUENCE, I PASS THE
// SIZE IN AS A SECOND PARAMETER, WHICH I'D RATHER NOT DO.
printf( "The size of p calculated = %ld.\n", SIZEOF( p ));
printf( "The size of p = %ld.\n", sizeof p );
printf( "The size of p[0] = %ld.\n", sizeof p[0] );
for( i = 0; i < s; i++ )
printf( "Eelement %d = %lf.\n", i, p[i] );
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
没有神奇的解决方案。 C 不是反射语言。对象不会自动知道它们是什么。
但你有很多选择:
There is no magic solution. C is not a reflective language. Objects don't automatically know what they are.
But you have many choices:
函数参数实际上从来没有数组类型。当编译器看到
或者甚至
将其中之一转换为时,
所以
sizeof(p)
是sizeof(double*)
。是的,您必须将大小作为参数传递。Function parameters never actually have array type. When the compiler sees
or even
it converts either one to
So
sizeof(p)
issizeof(double*)
. And yes, you'll have to pass the size as a parameter.问题是你的函数没有接收数组值;它接收一个指针值。
除非它是
sizeof
或一元&
运算符的操作数,或者它是用于初始化声明中另一个数组的字符串文字,类型为 " 的表达式array ofT
”将被转换为“pointer toT
”类型,其值将是数组第一个元素的地址。因此,当您调用
printarray
时,array1
的类型会从“100 元素的double
数组”隐式转换为“指向>双
。”因此,参数p
的类型是double *
,而不是double [100]
。在函数参数声明的上下文中,
T a[]
与T *a
相同。这就是为什么你必须单独传递数组大小;
The problem is that your function doesn't receive an array value; it receives a pointer value.
Except when it is the operand of the
sizeof
or unary&
operators, or it is a string literal being used to initialize another array in a declaration, an expression of type "array ofT
" will be converted to type "pointer toT
" and its value will be the address of the first element of the array.Thus, when you call
printarray
, the type ofarray1
is implicitly converted from "100-element array ofdouble
" to "pointer todouble
." Thus, the type of the parameterp
isdouble *
, notdouble [100]
.In the context of a function parameter declaration,
T a[]
is identical toT *a
.This is why you have to pass the array size separately;
你回答了你自己的问题。它是在编译时计算的,那么“sizeof p”在运行时怎么可能有多个值呢?
将长度作为参数传递是一个很好的解决方案,否则您可以确保数组始终以某些特殊值(如字符串和空字节)结尾。
You answered your own question. It's computed at compile-time, so how can 'sizeof p' possibly have more than one value during runtime?
Passing the length as a parameter is a fine solution, otherwise you can make sure your arrays always end with some special value (like strings and the null byte).
您必须
You must either
如果您的数组以 null 结尾,那么您可以迭代该数组并找到最后一个值。 C 中的许多字符串运算符都是这样工作的,
否则你没有太多选择。
sizeof 将返回 p 的大小,它是一个指针,等于您的 int 大小
If your array is null terminated then you can iterate through the array and find the last value. Many string operators in C work that way
Otherwise you don't have much choice.
sizeof will return the size of p, which is a pointer and will equals to your int size
您可以使用哨兵值,这是处理字符串的方式。缺点是您必须迭代整个数组才能找到它的大小。
例如,当您有一个字符串,例如:
您实际拥有的是一个长度为 7 的数组,第七个位置是一个空终止字符“\0”,用于指示数组/字符串的结尾。
另一种方法是创建一个结构体,其中包含一个大小,后跟数组。
You can use a sentinel value which is the way strings are dealt with. The drawback is that you have to iterate over the entire array to find it's size.
For example, when you have a string such as:
What you actually have is an array of length 7, with the seventh spot being a null terminating char '\0' which serves to indicate the end of the array / string.
Another way you could do it is to create a struct which consists of a size followed by the array.