声明指针函数返回数组实际上是合法的吗? [英] Declaring pointers to function returning arrays is actually legal?

查看:255
本文介绍了声明指针函数返回数组实际上是合法的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至少由C11标准,并从我读过。

在这里的返回类型是不允许是一个数组类型的唯一地方是在功能定义的一节(6.9.1.3在$):


  

函数的返回类型应是无效的或一个完整的对象类型
  比数组类型等。


在函数调用($ 6.5.2.2.1),它指出的:


  

这位前pression它表示所调用的函数应有类型
  指针函数返回无效或返回一个完整的对象类型
  不是一个数组类型的其他


这意味着,像这样的预期:

  INT(* PF1)()[4]; //法律?PF1(); //错误调用一个返回数组的函数

我的意思是,从如何我只知道标准的定义一个函数返回数组是非法的,而不是定义一个指针函数返回数组。证明我是错的,如果你能。另外,如果我错了,如果你解释我为什么是这句话在标准的话,我会很高兴?

虽然铛似乎并不这样认为,并会上升一个错误在上面的code,指出函数不能返回数组类型'诠释[4]。但是,这是一个真正的函数(和不情愿的指针一)?

修改

确定 - 我被标准纸功能说明符'的引用回答不能有返回类型数组。但是,如果我们使用typedef名称,而不是声明函数指针返回数组的 - 这会是合法的吗? -

 的typedef INT arr_t [4];arr_t(* PF1)(无效);

虽然我个人认为,这种情况下也被覆盖的答案,因为一个类型定义中定义的类型名称相同,我们明确地定义。


解决方案

,你发现这句话确实是只有函数定义,不是声明。但是,你错过了另一个约束:


  

6.7.5.3功能说明符(包括原型)


  
  

限制


  
  

1的函数声明不得指定返回类型是函数类型或数组类型。



  

另外,如果我错了,如果你解释我为什么是这句话在标准的话,我会很高兴?


需要有一个调用的函数返回一个完整的对象类型,因为函数声明允许其声明为返回一个不完整的类型的附加要求:

 结构S;
结构S F(); / * *有效/
无效克(){f()的; } / *无效* /
的struct {INT I; };
无效H(){f()的; } / * *有效/

这不是阵列。关于不是一个数组类型等的措辞只是为了确保数组不小心成为被允许在措辞是错误的。

At least by the C11 standard and from what I've read.

The only place where return type is not allowed to be an array type is in the section of function definitions (at $6.9.1.3):

The return type of a function shall be void or a complete object type other than array type.

At function calls ($6.5.2.2.1) it states this:

The expression that denotes the called function shall have type pointer to function returning void or returning a complete object type other than an array type.

Which means that something like this would be expected:

int (*pf1)()[4]; //legal?

pf1(); //error calling a function which return array

What I mean is that from how I understand the standard only defining a function returning arrays is illegal and not defining a pointer to function returning arrays. Prove me wrong if you can. Also if I'm wrong I would be happy if you explain me why is this sentence in the standard then?

Although clang doesn't seems to think that way and will rise an error in the above code stating that 'function cannot return array type 'int [4]''. But is this really a function (and not rather a pointer to one)?

EDIT:

OK - I was answered by citation of the standard paper that 'function declarators' can't have a return-type of array. However if we use a typedef name instead to declare a pointer to function returning arrays - would this be legal? -

typedef int arr_t[4];

arr_t (*pf1)(void);

Although I personally think that this case is also covered by the answers because the type-name defined by a 'typedef' is the same as one explicitly defined.

解决方案

The sentence that you found is indeed only about function definitions, not about declarations. However, you missed another constraint:

6.7.5.3 Function declarators (including prototypes)

Constraints

1 A function declarator shall not specify a return type that is a function type or an array type.

Also if I'm wrong I would be happy if you explain me why is this sentence in the standard then?

There needs to be an additional requirement that a called function returns a complete object type because a function declaration is allowed to declare it as returning an incomplete type:

struct S;
struct S f(); /* valid */
void g() { f(); } /* invalid */
struct S { int i; };
void h() { f(); } /* valid */

It's not about arrays. The wording about "other than an array type" is just to make sure arrays don't accidentally become allowed by a mistake in the wording.

这篇关于声明指针函数返回数组实际上是合法的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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