[問題] 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