且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么 C 和 C++ 编译器在从未强制执行时允许函数签名中的数组长度?

更新时间:2023-11-12 17:05:04

这是将数组传递给函数的语法的一个怪癖.

It is a quirk of the syntax for passing arrays to functions.

实际上,在 C 中不可能传递数组.如果您编写的语法看起来应该传递数组,实际发生的情况是传递了指向数组第一个元素的指针.

Actually it is not possible to pass an array in C. If you write syntax that looks like it should pass the array, what actually happens is that a pointer to the first element of the array is passed instead.

由于指针不包含任何长度信息,你的[]在函数形参列表中的内容实际上被忽略了.

Since the pointer does not include any length information, the contents of your [] in the function formal parameter list are actually ignored.

允许这种语法的决定是在 1970 年代做出的,从那时起就引起了很多混乱......

The decision to allow this syntax was made in the 1970s and has caused much confusion ever since...