为什么可以在函数参数中指定数组的大小?

时间:2022-07-01 02:41:50

I don't understand why the following example compiles and works:

我不明白为什么下面的例子编译和工作:

void printValues(int nums[3], int length) {
    for(int i = 0; i < length; i++) 
        std::cout << nums[i] << " ";
    std::cout << '\n';
}

It seems that the size of 3 is completely ignored but putting an invalid size results in a compile error. What is going on here?

似乎完全忽略了3的大小,但是放置无效的大小会导致编译错误。这里发生了什么?

3 个解决方案

#1


10  

In C++ (as well as in C), parameters declared with array type always immediately decay to pointer type. The following three declarations are equivalent

在C ++(以及C语言)中,使用数组类型声明的参数总是立即衰减为指针类型。以下三个声明是等效的

void printValues(int nums[3], int length);
void printValues(int nums[], int length);
void printValues(int *nums, int length);

I.e. the size does not matter. Yet, it still does not mean that you can use an invalid array declaration there, i.e. it is illegal to specify a negative or zero size, for example.

即大小无关紧要。然而,它仍然并不意味着你可以在那里使用无效的数组声明,例如,指定负数或零大小是非法的。

(BTW, the same applies to parameters of function type - it immediately decays to pointer-to-function type.)

(顺便说一下,这同样适用于函数类型的参数 - 它会立即衰减到指针到函数类型。)

If you want to enforce array size matching between arguments and parameters, use pointer- or reference-to-array types in parameter declarations

如果要强制参数和参数之间的数组大小匹配,请在参数声明中使用指针或引用数组类型

void printValues(int (&nums)[3]);
void printValues(int (*nums)[3]);

Of course, in this case the size will become a compile-time constant and there's no point of passing length anymore.

当然,在这种情况下,大小将成为编译时常量,并且不再有通过长度的点。

#2


2  

I don't see what compile error you are referring to - arrays passed to a function decay to pointers and you lose the array type information. You might as well have used:

我没有看到你所指的编译错误 - 传递给函数的数组衰减到指针而你丢失了数组类型信息。您可能也曾使用过:

void printValues(int* nums, int length);

You can avoid the decay to pointers by using references:

您可以通过使用引用来避免指针衰减:

void printValues(int (&nums)[3], int length);

Or simply use pointers if you don't want fixed-sized arrays.

或者,如果您不想要固定大小的数组,只需使用指针。

#3


-2  

The size of the array is not ignored, it is part of the type of the argument. You should get a compiler error if you try to pass an array of any other size into the function.

不忽略数组的大小,它是参数类型的一部分。如果您尝试将任何其他大小的数组传递给函数,则应该会出现编译器错误。

On the other hand C and C++ don't do bounds checking on array accesses, so in that sense they are ignored. But that's true in any other context as well, not just for function parameters.

另一方面,C和C ++不对数组访问进行边界检查,因此在这种意义上它们会被忽略。但在任何其他环境中也是如此,不仅仅是函数参数。

#1


10  

In C++ (as well as in C), parameters declared with array type always immediately decay to pointer type. The following three declarations are equivalent

在C ++(以及C语言)中,使用数组类型声明的参数总是立即衰减为指针类型。以下三个声明是等效的

void printValues(int nums[3], int length);
void printValues(int nums[], int length);
void printValues(int *nums, int length);

I.e. the size does not matter. Yet, it still does not mean that you can use an invalid array declaration there, i.e. it is illegal to specify a negative or zero size, for example.

即大小无关紧要。然而,它仍然并不意味着你可以在那里使用无效的数组声明,例如,指定负数或零大小是非法的。

(BTW, the same applies to parameters of function type - it immediately decays to pointer-to-function type.)

(顺便说一下,这同样适用于函数类型的参数 - 它会立即衰减到指针到函数类型。)

If you want to enforce array size matching between arguments and parameters, use pointer- or reference-to-array types in parameter declarations

如果要强制参数和参数之间的数组大小匹配,请在参数声明中使用指针或引用数组类型

void printValues(int (&nums)[3]);
void printValues(int (*nums)[3]);

Of course, in this case the size will become a compile-time constant and there's no point of passing length anymore.

当然,在这种情况下,大小将成为编译时常量,并且不再有通过长度的点。

#2


2  

I don't see what compile error you are referring to - arrays passed to a function decay to pointers and you lose the array type information. You might as well have used:

我没有看到你所指的编译错误 - 传递给函数的数组衰减到指针而你丢失了数组类型信息。您可能也曾使用过:

void printValues(int* nums, int length);

You can avoid the decay to pointers by using references:

您可以通过使用引用来避免指针衰减:

void printValues(int (&nums)[3], int length);

Or simply use pointers if you don't want fixed-sized arrays.

或者,如果您不想要固定大小的数组,只需使用指针。

#3


-2  

The size of the array is not ignored, it is part of the type of the argument. You should get a compiler error if you try to pass an array of any other size into the function.

不忽略数组的大小,它是参数类型的一部分。如果您尝试将任何其他大小的数组传递给函数,则应该会出现编译器错误。

On the other hand C and C++ don't do bounds checking on array accesses, so in that sense they are ignored. But that's true in any other context as well, not just for function parameters.

另一方面,C和C ++不对数组访问进行边界检查,因此在这种意义上它们会被忽略。但在任何其他环境中也是如此,不仅仅是函数参数。