[問題] function prototype scope

看板C_and_CPP (C/C++)作者 (brotherD)時間8年前 (2017/12/08 11:55), 8年前編輯推噓5(5015)
留言20則, 4人參與, 8年前最新討論串1/1
各位好,小弟最近在複習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來說,他們應該都是不存在 的。 謝謝前輩們看完小魯的發言~感恩感恩 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.26.157 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1512705349.A.842.html ※ 編輯: zzss2003 (60.248.26.157), 12/08/2017 11:56:08

12/08 12:25, 8年前 , 1F
就是函式宣告不用參數名稱 一句話
12/08 12:25, 1F

12/08 12:27, 8年前 , 2F
error那個是撞名的例子 因為compiler不能分辨送誰
12/08 12:27, 2F

12/08 12:28, 8年前 , 3F
但是error的例子下一個可以分辨 因為type不一樣
12/08 12:28, 3F

12/08 12:29, 8年前 , 4F
不好意思看錯 也是依樣 所以無法分辨A是誰
12/08 12:29, 4F

12/08 12:29, 8年前 , 5F
比如說今天function內有一行 x += a * A
12/08 12:29, 5F

12/08 12:30, 8年前 , 6F
送參數不一樣的時候 無法知道x要跟哪個a作用
12/08 12:30, 6F

12/08 12:31, 8年前 , 7F
比如使用f(2,3) 所以x+=a是要+2還是+3無法判斷
12/08 12:31, 7F

12/08 13:25, 8年前 , 8F
書上寫的ignores the name是個不夠精確的說法
12/08 13:25, 8F

12/08 13:27, 8年前 , 9F
實際上在它的scope裡面都有效,而且有時候真的會用到
12/08 13:27, 9F

12/08 13:28, 8年前 , 10F
例如說 auto add(T a, T b) -> decltype(a + b);
12/08 13:28, 10F
想請問一下,這段是什麼意思@@看不太懂:(

12/08 13:31, 8年前 , 11F
compiler不會忽略a, b這兩個變數名
12/08 13:31, 11F

12/08 13:32, 8年前 , 12F
是說你可以不寫, 但是只要寫了compiler就會去看
12/08 13:32, 12F

12/08 13:35, 8年前 , 13F
declarator 是定義在 c++ grammar 裡面的一個東西
12/08 13:35, 13F

12/08 13:36, 8年前 , 14F
簡單來說, int x = 0, y[100]; void foo(int z);
12/08 13:36, 14F

12/08 13:37, 8年前 , 15F
裡面的 x y[100] foo(int z) 叫做 declarator
12/08 13:37, 15F

12/08 14:37, 8年前 , 16F
找個 c 的規格書在手邊當字典可以幫你很多忙
12/08 14:37, 16F
話說我找不到C89的standard C,唯一找到的版本的字寫得很模糊...看得很不舒服 之前有網友說要找大陸的網址,但是我還是找不到QQ ※ 編輯: zzss2003 (60.248.26.157), 12/08/2017 16:03:42

12/08 16:24, 8年前 , 17F
啊, 因為 stackoverflow 那串讓我以為你在問 c++
12/08 16:24, 17F

12/08 16:24, 8年前 , 18F
忽略那行吧 XD
12/08 16:24, 18F

12/08 16:59, 8年前 , 19F
goggle c 9899 draft
12/08 16:59, 19F

12/12 13:36, 8年前 , 20F
You are reading too hard into the words
12/12 13:36, 20F
文章代碼(AID): #1QAWr5X2 (C_and_CPP)
文章代碼(AID): #1QAWr5X2 (C_and_CPP)