看板 C_and_CPP 關於我們 聯絡資訊
aecho:嗯嗯,那我有疑問~~08/19 05:51
aecho:void search(int **arr, int *p);08/19 05:51
arr is a pointer to a pointer to an int
aecho:void search(int *arr[COL], int *p);08/19 05:52
arr is an array of "a pointer to an int" type
aecho:void search(int arr[][COL], int *p);08/19 05:52
arr is a pointer to an int[COL](an int array type)
aecho:分別是怎麼被解釋的呢~~!?08/19 05:52
aecho:呃…我是想問那個arr會怎麼被解釋~~
其實有一定的規則可循,The C programming language(K&R)有提到這方法 從宣告名稱開始分析(即此處的arr) int * * (arr) -> arr is a/an int * (*arr) -> arr is a pointer int (**arr) -> arr is a pointer to a pointer (int **arr) -> arr is a pointer to a pointer to an int int *(arr)[COL] -> arr is a/an int *(arr[COL]) -> arr is an array(此處要注意[]的prior比*高) int (*arr[COL]) -> arr is an array of pointer (int *arr[COL]) -> arr is an array of pointer to an int int (arr)[][COL] -> arr is a/an int (arr[])[COL] -> arr is a pointer(在C裡arr[]等同於*arr,K&D裡建議用後者                    以明確指出是以pointer傳入) int (arr[][COL]) -> arr is a pointer to an array (int arr[][COL]) -> arr is a pointer to an array of int 在這裡還有一點要指出 arr[ROW][COL]和arr[][COL]不是同一種型態 前者是array type,可decay成int (*) [COL]型態,且無法改變其值(ex:++arr//error) 而arr[][COL]等同於int (*)[COL],可改變其值 array和pointer是不同type! -- 其他還有像是加上const修飾 一般我們會用const Type這種方式宣告一個constant obj 但是如果有很複雜型態的情況下 我比較prefer將const修飾加在後面 例如大家都知道: const int *const -> a const pointer to a const int 若是改成 int const *const ->同上 就可以很清楚看出來 const就是修飾於他前面的型態 如此一來 int const *const *const -> a const pointer to a const pointer to a const int int **const -> a const pointer to a pointer to a int int **const* -> a pointer to a const pointer to a pointer to a int 就可以很清楚的看出來了。 配合上typedef typedef int * PI; const PI -> const int*? int *const? 若是這樣看 PI const -> int *const typedef int const* PCI; PCI const -> int const *const typedef int const *const CPCI; CPCI const -> int const *const 這樣也好判斷多了 最後有關constant pointer型態轉換的問題 可以參考神人tinlans大的文章 #1B_2w2Uj 如果沒看到這個 還真一輩子不會發現有這問題... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.36.168.54
loveme00835:arr is an array of pointers to an int ←複數 08/19 08:52
loveme00835:不過這樣int也變成複數了...> < 08/19 08:55
我在最開始加了個註解 "a pointer to an int"是一種type 而arr是此type的array...
loveme00835:array 那邊第三行不知道合不合理, 我是這樣看 08/19 09:00
loveme00835:(int*) (arr[COL]); xd 08/19 09:01
loveme00835:偷偷提醒a大, 型態是從右至左來看的, h 大也是如此 08/19 09:05
其實我比較喜歡用"從identifier開始,再判斷結合的prior"來說明...像[]就是往右結合
loveme00835:嗯嗯, 推用心 XD 08/19 09:30
hilorrk:分段撰寫 P幣少賺了好多啊啊啊啊!!! 08/19 09:46
※ 編輯: hilorrk 來自: 114.36.168.54 (08/19 10:58)
LPH66:說起來這裡倒沒有什麼結合的 prior...純粹是先右再左而已 08/19 12:33
LPH66:只是在左右兩邊的東西都是有順序的 所以要這樣看也是沒差 08/19 12:33
aecho:推P幣少賺了好多XDD 08/19 12:50
aecho:感恩~~ ^^ 之前都沒背這個解釋的法則…吃了不少苦頭 囧 08/19 12:51
tomnelson:這篇要m起來! 08/19 18:23
aecho:應該有結合的prior吧!? 08/19 19:32
aecho:void search(int *arr[COL],int *p); 08/19 19:33
aecho:void search(int (*arr)[COL],int *p); 08/19 19:33
aecho:上面這兩個會不一樣....早上在測的時候發現的 08/19 19:33
aecho:喔,我爽笨了~~ Orz 08/19 19:34
adxis:g++ -S 讀一下就知道了哪裡不一樣了 08/20 11:31