Compatible types and argument type qualifiers

白昼怎懂夜的黑 提交于 2019-12-12 11:26:57

问题


Are the types of these two declarations compatible types?

void f(char *, char *);
void f(char *restrict, char *restrict);

or similarly:

void g(char *);
void g(char *const);

I'm having a hard time finding anything in the standard which covers the issue. I'm mostly interested in the topic of whether it's valid to manually prototype a function, omitting the restrict keyword, where the actual type might have restrict-qualified arguments depending on the version of C or version of other libraries in use.


回答1:


C11 section 6.7.6.3 §15:

In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.




回答2:


They are compatible:

(C99, 6.7.5.3 Function declarators (including prototypes) p15) "[...] (In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.)"




回答3:


The names of the arguments in the prototype doesn't matter, so these definitions are equivalent. However it's a good practice, to put the names, as these should give some idea what the arguments are intended for. Technically they are not need though, but serve as a documentation.

It is a different matter with the constqualifier, because this changes the meaning of the function.



来源:https://stackoverflow.com/questions/18215686/compatible-types-and-argument-type-qualifiers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!