为什么不与MAIN中的数组参数的大小相同?
为什么数组的大小不与MAIN中的参数相同?
#include <stdio.h>
void PrintSize(int p_someArray[10]);
int main () {
int myArray[10];
printf("%d\n", sizeof(myArray)); /* As expected, 40 */
PrintSize(myArray);/* Prints 4, not 40 */
}
void PrintSize(int p_someArray[10]){
printf("%d\n", sizeof(p_someArray));
}
Why isn't the size of an array sent as a parameter the same as within main?
#include <stdio.h>
void PrintSize(int p_someArray[10]);
int main () {
int myArray[10];
printf("%d\n", sizeof(myArray)); /* As expected, 40 */
PrintSize(myArray);/* Prints 4, not 40 */
}
void PrintSize(int p_someArray[10]){
printf("%d\n", sizeof(p_someArray));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您发现的行为实际上是C语言中的大疣。每当您声明采用数组参数的函数时,编译器都会忽略您,并将参数更改为指针。因此,这些声明的行为就像第一个:
A将是在所有四种情况下的指针。如果您将数组传递到Func,它将立即腐烂到指向其第一个元素的指针。 (在64位系统上,一个64位指针的大小是32位INT的两倍,因此您的大小比率返回2。)
该规则的唯一目的是与不支持的历史编译器保持向后兼容将聚合值作为函数参数传递。
这并不意味着不可能将数组传递给函数。您可以通过将数组嵌入结构(基本上是C ++ 11的STD ::数组的目的)来绕开此疣:
或者通过将指针传递到数组:
如果数组大小不是编译 -时间常数,您可以使用C99可变长度数组的指针到阵列技术:
The behavior you found is actually a big wart in the C language. Whenever you declare a function that takes an array parameter, the compiler ignores you and changes the parameter to a pointer. So these declarations all behave like the first one:
a will be a pointer to int in all four cases. If you pass an array to func, it will immediately decay into a pointer to its first element. (On a 64-bit system, a 64-bit pointer is twice as large as a 32-bit int, so your sizeof ratio returns 2.)
The only purpose of this rule is to maintain backwards compatibility with historical compilers that did not support passing aggregate values as function arguments.
This does not mean that it’s impossible to pass an array to a function. You can get around this wart by embedding the array into a struct (this is basically the purpose of C++11’s std::array):
or by passing a pointer to the array:
In case the array size isn’t a compile-time constant, you can use the pointer-to-array technique with C99 variable-length arrays:
因为阵列作为参数传递时将其腐烂成指针。这就是C起作用的方式,尽管您可以通过参考将C ++中的“数组”传递并克服此问题。请注意,您可以将不同尺寸的数组传递给此功能:
Because arrays decay into pointers when they are passed as parameters. This is how C works, although you can pass "arrays" in C++ by reference and overcome this issue. Note that you can pass arrays of different sizes to this function:
在C ++中,您可以为此目的通过引用传递数组:
In c++ you can pass an array by reference for this very purpose :
在C语言中,没有方法可以确定
数组的大小,因此数量需要
通过以及指向第一个元素的指针。
In the C language, there is no method to determine the
size of an unknown array, so the quantity needs to
be passed as well as a pointer to the first element.
您不能将数组传递给函数。
如果您真的想打印大小,则可以将指针传递给数组,但是它根本不通用,因为您还需要定义该功能的数组尺寸。
You can't pass arrays to functions.
If you really wanted to print the size, you could pass a pointer to an array, but it won't be generic at all as you need to define the array size for the function as well.
行为是设计。
功能参数声明中的同一语法是指与局部变量定义中完全不同的内容。
原因在其他答案中描述。
The behavior is by design.
Same syntax in function parameter declaration means completely different thing than in local variable definition.
The reason is described in other answers.
在C语言中,当您将数组作为参数传递给该函数时,它会自动转换为指针,从一个函数传递另一个函数的数组通过参考将其称为呼叫。这就是所谓函数仅接收指向函数的第一个元素的指针的原因,这就是
Fun(int a [])类似于娱乐(int *a)的原因;
因此,当您打印数组的大小时,它将打印第一个元素的大小。
In C language when you pass the array as an argument to the function , it is automatically converted into pointer ,array passing from one function other function is know as call by reference . That is the reason the called function only receives the pointer which point to the first element of function This is the reason
fun(int a[]) is similar to fun(int *a) ;
so when you print the size of array it will print the size of first element.
在“ c”编程langlyange'sizeof()'是运算符,他返回字节中的对象大小。“ sizeof()”操作员的观点必须是左值类型(整数,浮点数,float号,struct,struct,array,array )。因此,如果您想知道字节中的数组的大小,您可以非常简单。仅使用'sizeof()'operator',并且为他的参数使用阵列名称。例如:
32位系统上的输出将是:n的尺寸为:40。因为32系统上的Ineteger是4bytes.on 64x,是8bytes。在这种情况下,我们在一个数组中声明了10个整数。因此,结果是'10 * sizeof(int)'。
一些提示:
如果我们有一个像这样声明的数组,那就是'int n [] = {1,2,3,... 155 ..};'。
因此,我们想知道该数组中存储了多少个元素。
使用此alghorithm:
sizeof(name_of_the_array) / sizeof(array_type)
代码:#include
main(){
} { }
In 'C' programming languange 'sizeof()' is the operator and he returns the size of the object in bytes.Argument of the 'sizeof()' operator must be a left-value type(integer,float number,struct,array).So if you want to know the size of an array in bytes you can do it very simple.Just use the 'sizeof()' operator and for his argument use the array name.For example:
Output on 32 bit system will be: Size of n is: 40.Because ineteger on 32 system is 4bytes.On 64x it is 8bytes.In this case we have 10 integers declared in one array.So the result is '10 * sizeof(int)'.
Some tips:
If we have an array declared like this one 'int n[]={1, 2, 3, ...155..};'.
So we want to know how many elements are stored in this array.
Use this alghorithm:
sizeof(name_of_the_array) / sizeof(array_type)
Code: #include
main(){
}
阵列仅大小。在大多数情况下,数组是通往内存的指针。声明中的大小仅告诉编译器为该数组分配多少内存 - 它与类型无关,因此SizeOf()无需进行。
Arrays are only loosely sized. For the most part, an array is a pointer to memory. The size in your declaration only tells the compiler how much memory to allocate for the array - it's not associated with the type, so sizeof() has nothing to go on.
当您将其传递给函数时,数组类型是隐式转换为指针类型。
因此,
是
等效的。因此,您得到的是
sizeof(int*)
的值An array-type is implicitly converted into pointer type when you pass it in to a function.
So,
and
are equivalent. So what you get is the value of
sizeof(int*)
这是一个指针,这就是为什么将数组的大小作为第二个参数传递给函数的常见实现
It's a pointer, that's why it's a common implementation to pass the size of the array as a second parameter to the function
正如其他人所说的那样,当用作函数参数时,阵列会衰减指向其第一个元素。还值得注意的是,SizeOf不会评估表达式,并且与表达式一起使用时不需要括号,因此您的参数实际上根本没有使用,因此您不妨使用类型而不是值编写大小。
As others have stated, arrays decay to pointers to their first element when used as function parameters. It's also worth noting that sizeof does not evaluate the expression and does not require parentheses when used with an expression, so your parameter isn't actually being used at all, so you may as well write the sizeof with the type rather than the value.
因此,您需要将数组的Lenght作为第二个参数传递。当您编写代码时,您俩都声明了一个恒定大小的数组,然后将该数组传递给函数时,将数组长度常数显示在您的代码中的几个位置...
k&amp; r 是很痛苦的。为了营救:
所以现在您可以做:
So, you will need to pass the lenght of the array as a second parameter. When you are writing code, in which you both declare an array of constant size, and later pass that array to a function, it is a pain to have the array-length constant show up several places in your code...
K&R to the rescue:
So now you can do e.g: