加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C原型范围

发布时间:2020-12-16 09:25:16 所属栏目:百科 来源:网络整理
导读:我了解到了 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 fu
我了解到了

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读