[问题] function prototype scope

楼主: zzss2003 (brotherD)   2017-12-08 11:55:46
各位好,小弟最近在复习C How to Program 6/E,看到了function prototype scope这边
图:https://imgur.com/a/FvvlT
中文说明大致上为:用在function prototype里面的参数有function prototype scope,然
后接着说:其实这个参数是不用打上识别字的,只需要型别就行,就算打上去的话,也会被
compiler忽略,所以如果在程式里面宣告了跟参数一样的识别字也不会有问题。
坦白讲,小弟觉得有讲跟没讲一样,所以上网google了一下这篇讨论串(PS:我没学过C++)
讨论串:
https://stackoverflow.com/questions/24393273/example-of-function-prototype-scope
看了一下发现其实不是很懂,故发文上来请教前辈们。
其中一开始的内文写道:In a function declaration, or in any function declarator..
想请问,这边的declarator是指什么东西?第一次看到这个名词,上网查好像是C++才有?
接着,The best answer说明了: The compiler will issue an error for the second
parameter a because its name coincides with the name of the first parameter.
问题: Compiler不是会忽略parameter的name吗?为什么还会issue an error?
接着The best answer又说: The same name is defined twice in the same scope.
问题: 我听起来的直觉是,function prototyp scope的范围就只是在function prototype
里面而已,是这样吗?
接着,The best answer又说明了一个范例,并说明第一个parameter name把struct name
给藏起来了,所以struct name是使用详尽的结构名称定义的。
问题: 为什么第一个参数会把第二个参数给藏起来?对Compiler来说,他们应该都是不存在
的。
谢谢前辈们看完小鲁的发言~感恩感恩
作者: Sidney0503 (Sidney0503)   2017-12-08 12:25:00
就是函式宣告不用参数名称 一句话error那个是撞名的例子 因为compiler不能分辨送谁但是error的例子下一个可以分辨 因为type不一样不好意思看错 也是依样 所以无法分辨A是谁比如说今天function内有一行 x += a * A送参数不一样的时候 无法知道x要跟哪个a作用比如使用f(2,3) 所以x+=a是要+2还是+3无法判断
作者: AstralBrain   2017-12-08 13:25:00
书上写的ignores the name是个不够精确的说法实际上在它的scope里面都有效,而且有时候真的会用到例如说 auto add(T a, T b) -> decltype(a + b);compiler不会忽略a, b这两个变量名是说你可以不写, 但是只要写了compiler就会去看declarator 是定义在 c++ grammar 里面的一个东西简单来说, int x = 0, y[100]; void foo(int z);里面的 x y[100] foo(int z) 叫做 declarator
作者: notBeing (read and be read)   2017-12-08 14:37:00
找个 c 的规格书在手边当字典可以帮你很多忙
作者: AstralBrain   2017-12-08 16:24:00
啊, 因为 stackoverflow 那串让我以为你在问 c++忽略那行吧 XD
作者: notBeing (read and be read)   2017-12-08 16:59:00
goggle c 9899 draft
作者: MOONRAKER (㊣牛鹤鳗毛人)   2017-12-12 13:36:00
You are reading too hard into the words

Links booklink

Contact Us: admin [ a t ] ucptt.com