为什么不与MAIN中的数组参数的大小相同?

发布于 2025-02-07 18:36:38 字数 341 浏览 1 评论 0原文

为什么数组的大小不与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

帅气称霸 2025-02-14 18:36:39

您发现的行为实际上是C语言中的大疣。每当您声明采用数组参数的函数时,编译器都会忽略您,并将参数更改为指针。因此,这些声明的行为就像第一个:

void func(int *a)
void func(int a[])
void func(int a
typedef int array_plz[5];
void func(array_plz a)

A将是在所有四种情况下的指针。如果您将数组传递到Func,它将立即腐烂到指向其第一个元素的指针。 (在64位系统上,一个64位指针的大小是32位INT的两倍,因此您的大小比率返回2。)

该规则的唯一目的是与不支持的历史编译器保持向后兼容将聚合值作为函数参数传递。

这并不意味着不可能将数组传递给函数。您可以通过将数组嵌入结构(基本上是C ++ 11的STD ::数组的目的)来绕开此疣:

struct array_rly {
int a[5];
};
void func(struct array_rly a)
{
printf("%zd\n", sizeof(a.a)/sizeof(a.a[0]));  /* prints 5 */
}

或者通过将指针传递到数组:

void func(const int (*a)[5])
{
printf("%zd\n", sizeof(*a)/sizeof((*a)[0]));  /* prints 5 */
}

如果数组大小不是编译 -时间常数,您可以使用C99可变长度数组的指针到阵列技术:

void func(int n, const int (*a)[n])
{
printf("%zd\n", sizeof(*a)/sizeof((*a)[0]));  /* prints n */
}

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:

void func(int *a)
void func(int a[])
void func(int a
typedef int array_plz[5];
void func(array_plz a)

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):

struct array_rly {
int a[5];
};
void func(struct array_rly a)
{
printf("%zd\n", sizeof(a.a)/sizeof(a.a[0]));  /* prints 5 */
}

or by passing a pointer to the array:

void func(const int (*a)[5])
{
printf("%zd\n", sizeof(*a)/sizeof((*a)[0]));  /* prints 5 */
}

In case the array size isn’t a compile-time constant, you can use the pointer-to-array technique with C99 variable-length arrays:

void func(int n, const int (*a)[n])
{
printf("%zd\n", sizeof(*a)/sizeof((*a)[0]));  /* prints n */
}
坐在坟头思考人生 2025-02-14 18:36:39

因为阵列作为参数传递时将其腐烂成指针。这就是C起作用的方式,尽管您可以通过参考将C ++中的“数组”传递并克服此问题。请注意,您可以将不同尺寸的数组传递给此功能:

 // 10 is superfluous here! You can pass an array of different size!
void PrintSize(int p_someArray[10]);

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:

 // 10 is superfluous here! You can pass an array of different size!
void PrintSize(int p_someArray[10]);
梦境 2025-02-14 18:36:39

在C ++中,您可以为此目的通过引用传递数组:

void foo(int (&array)[10])
{
    std::cout << sizeof(array) << "\n";
}

In c++ you can pass an array by reference for this very purpose :

void foo(int (&array)[10])
{
    std::cout << sizeof(array) << "\n";
}
浅紫色的梦幻 2025-02-14 18:36:39

在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.

寄居者 2025-02-14 18:36:39

您不能将数组传递给函数。

如果您真的想打印大小,则可以将指针传递给数组,但是它根本不通用,因为您还需要定义该功能的数组尺寸。

#include <stdio.h>

void PrintSize(int (*p_anArray)[10]);

int main(void) {
    int myArray[10];
    printf("%d\n", sizeof(myArray)); /* as expected 40 */
    PrintSize(&myArray);/* prints 40 */
}

void PrintSize(int (*p_anArray)[10]){
    printf("%d\n", (int) sizeof(*p_anArray));
}

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.

#include <stdio.h>

void PrintSize(int (*p_anArray)[10]);

int main(void) {
    int myArray[10];
    printf("%d\n", sizeof(myArray)); /* as expected 40 */
    PrintSize(&myArray);/* prints 40 */
}

void PrintSize(int (*p_anArray)[10]){
    printf("%d\n", (int) sizeof(*p_anArray));
}
年少掌心 2025-02-14 18:36:39

行为是设计。

功能参数声明中的同一语法是指与局部变量定义中完全不同的内容。

原因在其他答案中描述。

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.

烟花易冷人易散 2025-02-14 18:36:39

在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.

栩栩如生 2025-02-14 18:36:39

在“ c”编程langlyange'sizeof()'是运算符,他返回字节中的对象大小。“ sizeof()”操作员的观点必须是左值类型(整数,浮点数,float号,struct,struct,array,array )。因此,如果您想知道字节中的数组的大小,您可以非常简单。仅使用'sizeof()'operator',并且为他的参数使用阵列名称。例如:

#include <stdio.h>

main(){

 int n[10];
 printf("Size of n is: %d \n", sizeof(n)); 

}

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(){

int n[] = { 1, 2, 3, 44, 6, 7 };
printf("Number of elements: %d \n", sizeof(n) / sizeof(int));
return 0;

} { }

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:

#include <stdio.h>

main(){

 int n[10];
 printf("Size of n is: %d \n", sizeof(n)); 

}

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(){

int n[] = { 1, 2, 3, 44, 6, 7 };
printf("Number of elements: %d \n", sizeof(n) / sizeof(int));
return 0;

}

未央 2025-02-14 18:36:39

阵列仅大小。在大多数情况下,数组是通往内存的指针。声明中的大小仅告诉编译器为该数组分配多少内存 - 它与类型无关,因此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.

羞稚 2025-02-14 18:36:38

当您将其传递给函数时,数组类型是隐式转换为指针类型。

因此,

void PrintSize(int p_someArray[10]) {
    printf("%zu\n", sizeof(p_someArray));
}

void PrintSize(int *p_someArray) {
    printf("%zu\n", sizeof(p_someArray));
}

等效的。因此,您得到的是 sizeof(int*) 的值

An array-type is implicitly converted into pointer type when you pass it in to a function.

So,

void PrintSize(int p_someArray[10]) {
    printf("%zu\n", sizeof(p_someArray));
}

and

void PrintSize(int *p_someArray) {
    printf("%zu\n", sizeof(p_someArray));
}

are equivalent. So what you get is the value of sizeof(int*)

雨巷深深 2025-02-14 18:36:38

这是一个指针,这就是为什么将数组的大小作为第二个参数传递给函数的常见实现

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

恋你朝朝暮暮 2025-02-14 18:36:38

正如其他人所说的那样,当用作函数参数时,阵列会衰减指向其第一个元素。还值得注意的是,SizeOf不会评估表达式,并且与表达式一起使用时不需要括号,因此您的参数实际上根本没有使用,因此您不妨使用类型而不是值编写大小。

#include <stdio.h>

void PrintSize1 ( int someArray[][10] );
void PrintSize2 ( int someArray[10] );

int main ()
{
    int myArray[10];
    printf ( "%d\n", sizeof myArray ); /* as expected 40 */
    printf ( "%d\n", sizeof ( int[10] ) ); /* requires parens */
    PrintSize1 ( 0 ); /* prints 40, does not evaluate 0[0] */
    PrintSize2 ( 0 ); /* prints 40, someArray unused */
}

void PrintSize1 ( int someArray[][10] )
{
    printf ( "%d\n", sizeof someArray[0] );
}

void PrintSize2 ( int someArray[10] )
{
    printf ( "%d\n", sizeof ( int[10] ) );
}

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.

#include <stdio.h>

void PrintSize1 ( int someArray[][10] );
void PrintSize2 ( int someArray[10] );

int main ()
{
    int myArray[10];
    printf ( "%d\n", sizeof myArray ); /* as expected 40 */
    printf ( "%d\n", sizeof ( int[10] ) ); /* requires parens */
    PrintSize1 ( 0 ); /* prints 40, does not evaluate 0[0] */
    PrintSize2 ( 0 ); /* prints 40, someArray unused */
}

void PrintSize1 ( int someArray[][10] )
{
    printf ( "%d\n", sizeof someArray[0] );
}

void PrintSize2 ( int someArray[10] )
{
    printf ( "%d\n", sizeof ( int[10] ) );
}
空心空情空意 2025-02-14 18:36:38

因此,您需要将数组的Lenght作为第二个参数传递。当您编写代码时,您俩都声明了一个恒定大小的数组,然后将该数组传递给函数时,将数组长度常数显示在您的代码中的几个位置...

k&amp; r 是很痛苦的。为了营救:

#define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0])) 

所以现在您可以做:

int a[10];
...
myfunction(a, N_ELEMENTS(a));

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:

#define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0])) 

So now you can do e.g:

int a[10];
...
myfunction(a, N_ELEMENTS(a));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文