a [] []和(* a)[]不等同于函数参数吗? [英] Aren't a[][] and (*a)[] equivalent as function parameters?

查看:96
本文介绍了a [] []和(* a)[]不等同于函数参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

功能原型

void foo(int n, int a[][]);

给出了有关时不完整类型的错误

gives error about incomplete type while

void foo(int n, int (*a)[]);  

编译.根据衰减规则,在这种情况下,int a[][]等效于int (*a)[],因此int (*a)[]也会给出关于不完整类型的错误,但GCC似乎接受了它.有什么我想念的吗? 这可能是GCC的错误,但我没有找到任何相关的信息.

compiles. As per the decay rule int a[][] is equivalent to int (*a)[] in this case and therefore int (*a)[] should also give an error about an incomplete type but GCC seems to accept it. Is there anything I am missing? This might be a GCC bug but I didn't find anything related to it.

推荐答案

否,它们不等同于函数参数.它们并不等同于与foobar

No, they are not equivalent as function parameters. They are not equivalent in exactly the same way as parameter declarations in foo and bar

struct S;
void foo(struct S* s); // OK
void bar(struct S a[]); // ERROR: incomplete type is not allowed

不相同.

C不允许将不完整类型作为数组元素(请参见C 1999 6.7.5.2/1:"[...]元素类型不得为不完整或函数类型.[...]")),并且此限制适用于数组参数声明的方式与适用于任何其他数组声明的方式相同.即使以后将数组类型的参数隐式地调整为指针类型,C也不会对函数参数列表中的数组声明提供任何特殊处理.换句话说,在上述调整之前 ,检查数组参数声明的有效性.

C does not allow incomplete types as array elements (see C 1999 6.7.5.2/1: "[...] The element type shall not be an incomplete or function type. [...]") and this restriction applies to array parameter declarations the same way as it applies to any other array declarations. Even though parameters of array type will be later implicitly adjusted to pointer type, C simply provides no special treatment for array declarations in function parameter lists. In other words, array parameter declarations are checked for validity before the aforementioned adjustment.

您的int a[][]是同一件事:尝试声明元素类型为int []的数组,这是一个不完整的类型.同时,int (*a)[]是完全合法的-指向不完整类型的指针并没有什么异常.

Your int a[][] is the same thing: an attempt to declare an array with elements of type int [], which is an incomplete type. Meanwhile, int (*a)[] is perfectly legal - there's nothing unusual about pointers to incomplete types.

作为附带说明,C ++修复"了此问题,允许在参数声明中使用不完整类型的数组.但是,原始的C ++仍然禁止int a[][]参数,int (&a)[]参数甚至int (*a)[]参数.据说该问题已在C ++ 17中稍后得到修复/允许( http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#393 )

As a side note, C++ "fixed" this issue, allowing arrays of incomplete type in parameter declarations. However, the original C++ still prohibits int a[][] parameters, int (&a)[] parameters and even int (*a)[] parameters. This was supposedly fixed/allowed later in C++17 (http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#393)

这篇关于a [] []和(* a)[]不等同于函数参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆