the type specifIEr that declares the
IDentifIEr in the List of parameter
declarations in a function prototype
(not part of a function deFinition),
the IDentifIEr has function prototype
scope,which terminates at the end of
the function declarator.
请参阅下面提到的C程序.
voID fn (struct st {int a;} a,struct st b) ;struct st obj ;
编译器会立即发出错误,因为’obj’大小未知(或)struct st不是’type’.那就对了!结构’struct st’的声明在原型声明中结束.
我相信原型有这个限制因为我们也可以在原型声明中使用一些变量名.这些名称可能与同一范围内的变量(与函数原型的变量)冲突.如下.
voID fn (int a) ;int a ;
因此,为了允许上述声明,原型的范围是有限的. (如果我错了,请纠正我)
但是,对于原型声明,参数变量名称没有用.那么,为什么它是“狭隘的范围”?具有参数变量名称的意义是什么?语言设计者(或)规范对此的想法是什么?
解决方法 参数名称可以帮助记录参数的使用.考虑一个内存设置功能:
voID mem_set(voID *,int,int);voID mem_set(voID *buffer,int value,int nbytes);
哪个更容易理解?
写入的结构类型声明是函数原型的本地.您可能知道(现在,如果不是之前),您需要定义原型范围之外的类型才能成功使用它.也就是说,你必须写:
struct st {int a;};voID fn(struct st a,struct st b);
你说:
I belIEve prototype had this limit because we can use some variable names in the prototype declarations too. These names may conflict with the variables in the same scope (as that of function prototype). like below.
06002
So,to allow the above declarations the scope of prototype is limited. (Correct me if I am wrong)
有’-Wshadow’的GCC可能会警告参数’a’遮蔽全局变量’a’ – 它肯定会在函数定义中这样做,并且可能在函数声明中这样做.但这不是强制性警告;所写的代码是合法的C – 虽然因阴影而略显可疑.
关于“为什么C限制(阻止)你在参数列表中声明一个类型”的评论中有一个旷日持久的讨论,子文本“因为C允许你这样做”:
评论
Being allowed of /**/,it should be the programmer’s responsibility (as per Coding practices) to add proper comments about the use of the language there. I belIEve there has to be ‘something’ other than provIDing assistance to comments. – Ganesh Gopalasubramanian
OK – belIEve away. Compatibility with what C++ dID was the rest of the reason,and the argument names were added there to promote readability. See Stroustrup ‘Design and Evolution of C++’. Note that the names of the parameters in the prototype are not part of the interface – see the discussion on provIDing arguments by name instead of position. – Jonathan Leffler
I belIEve the question the OP is asking is “what’s the point of having function prototype scope at all?”. You answer,unfortunately,does not shed any light on it. Frankly,I have no IDea either. If they simply wanted to limit the scope of named parameter declarations (in a non-defining declaration) as OP guesses,they Could’ve done it without introducing a scope (as it is done in C++ for example). – AndreyT
@AndreyT: in a function deFinition,the arguments are local to the body of the function (it is no longer possible to hIDe an argument by a local variable in the outermost block of the body of the function). The prototype logically declares a type insIDe the function,and therefore should be scoped as defined – and hence,a type defined only in the function prototype cannot be used outsIDe the function,and a function that cannot be called is of little benefit to anyone. – Jonathan Leffler
@Jonathan Leffler: You seem to be explaining why the parameter names were allowed (“compatibility with C++” – OK). What I’d like to kNow is the rationale for introducing the “function prototype scope”. Why dID they see the need to introduce such a scope? C++ doesn’t do it that way. There’s no function prototype scope in C++. – AndreyT
@AndreyT Yeh! We both are drowning in the same boat 总结
以上是内存溢出为你收集整理的C原型范围全部内容,希望文章能够帮你解决C原型范围所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)