声明指向函数返回数组的指针实际上是合法的吗?
Declaring pointers to function returning arrays is actually legal?
至少根据 C11 标准和我读过的内容。
唯一不允许 return 类型为数组类型的地方是函数定义部分(在 $6.9.1.3):
The return type of a function shall be void or a complete object type
other than array type.
在函数调用($6.5.2.2.1)时,它指出:
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.
这意味着会出现这样的情况:
int (*pf1)()[4]; //legal?
pf1(); //error calling a function which return array
我的意思是,根据我对标准的理解,仅定义一个函数return数组是非法的,而不是定义一个指向函数的指针return数组。如果可以的话,证明我是错的。另外,如果我错了,如果你能解释一下为什么这句话在标准中,我会很高兴?
尽管 clang 似乎并不这么认为,并且会在上面的代码中引发错误,指出“函数不能 return 数组类型 'int [4]'”。但这真的是一个函数(而不是指向函数的指针)吗?
编辑:
OK - 标准论文的引用回答了我 'function declarators' 不能有 return 类型的数组。但是,如果我们使用 typedef 名称来声明指向函数 returning 数组的指针——这合法吗? -
typedef int arr_t[4];
arr_t (*pf1)(void);
尽管我个人认为答案也涵盖了这种情况,因为 'typedef' 定义的类型名称与明确定义的类型名称相同。
Declaring pointers to function returning arrays is actually legal?
没有。这是不合法的。你会得到一个编译时错误。函数不能 return 数组。
暂时,如果int (*pf1)()[4];
无论如何都有效,那么函数调用
pf1();
没有任何意义。 pf1
没有指向任何函数。
你查到的这句话确实只是关于函数定义的,而不是关于声明的。但是,您错过了另一个限制条件:
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?
需要额外要求被调用的函数returns是完整的对象类型,因为允许函数声明将其声明为返回不完整的类型:
struct S;
struct S f(); /* valid */
void g() { f(); } /* invalid */
struct S { int i; };
void h() { f(); } /* valid */
这与数组无关。关于“非数组类型”的措辞只是为了确保数组不会因措辞错误而意外地被允许。
至少根据 C11 标准和我读过的内容。
唯一不允许 return 类型为数组类型的地方是函数定义部分(在 $6.9.1.3):
The return type of a function shall be void or a complete object type other than array type.
在函数调用($6.5.2.2.1)时,它指出:
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.
这意味着会出现这样的情况:
int (*pf1)()[4]; //legal?
pf1(); //error calling a function which return array
我的意思是,根据我对标准的理解,仅定义一个函数return数组是非法的,而不是定义一个指向函数的指针return数组。如果可以的话,证明我是错的。另外,如果我错了,如果你能解释一下为什么这句话在标准中,我会很高兴?
尽管 clang 似乎并不这么认为,并且会在上面的代码中引发错误,指出“函数不能 return 数组类型 'int [4]'”。但这真的是一个函数(而不是指向函数的指针)吗?
编辑:
OK - 标准论文的引用回答了我 'function declarators' 不能有 return 类型的数组。但是,如果我们使用 typedef 名称来声明指向函数 returning 数组的指针——这合法吗? -
typedef int arr_t[4];
arr_t (*pf1)(void);
尽管我个人认为答案也涵盖了这种情况,因为 'typedef' 定义的类型名称与明确定义的类型名称相同。
Declaring pointers to function returning arrays is actually legal?
没有。这是不合法的。你会得到一个编译时错误。函数不能 return 数组。
暂时,如果int (*pf1)()[4];
无论如何都有效,那么函数调用
pf1();
没有任何意义。 pf1
没有指向任何函数。
你查到的这句话确实只是关于函数定义的,而不是关于声明的。但是,您错过了另一个限制条件:
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?
需要额外要求被调用的函数returns是完整的对象类型,因为允许函数声明将其声明为返回不完整的类型:
struct S;
struct S f(); /* valid */
void g() { f(); } /* invalid */
struct S { int i; };
void h() { f(); } /* valid */
这与数组无关。关于“非数组类型”的措辞只是为了确保数组不会因措辞错误而意外地被允许。