Re: [語法] pointer to array請教

看板C_and_CPP作者 (Cary)時間15年前 (2010/08/19 08:37), 編輯推噓4(4014)
留言18則, 6人參與, 最新討論串3/4 (看更多)

08/19 05:51,
嗯嗯,那我有疑問~~
08/19 05:51

08/19 05:51,
void search(int **arr, int *p);
08/19 05:51
arr is a pointer to a pointer to an int

08/19 05:52,
void search(int *arr[COL], int *p);
08/19 05:52
arr is an array of "a pointer to an int" type

08/19 05:52,
void search(int arr[][COL], int *p);
08/19 05:52
arr is a pointer to an int[COL](an int array type)

08/19 05:52,
分別是怎麼被解釋的呢~~!?
08/19 05:52

呃…我是想問那個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

08/19 08:52, , 1F
arr is an array of pointers to an int ←複數
08/19 08:52, 1F

08/19 08:55, , 2F
不過這樣int也變成複數了...> <
08/19 08:55, 2F
我在最開始加了個註解 "a pointer to an int"是一種type 而arr是此type的array...

08/19 09:00, , 3F
array 那邊第三行不知道合不合理, 我是這樣看
08/19 09:00, 3F

08/19 09:01, , 4F
(int*) (arr[COL]); xd
08/19 09:01, 4F

08/19 09:05, , 5F
偷偷提醒a大, 型態是從右至左來看的, h 大也是如此
08/19 09:05, 5F
其實我比較喜歡用"從identifier開始,再判斷結合的prior"來說明...像[]就是往右結合

08/19 09:30, , 6F
嗯嗯, 推用心 XD
08/19 09:30, 6F

08/19 09:46, , 7F
分段撰寫 P幣少賺了好多啊啊啊啊!!!
08/19 09:46, 7F
※ 編輯: hilorrk 來自: 114.36.168.54 (08/19 10:58)

08/19 12:33, , 8F
說起來這裡倒沒有什麼結合的 prior...純粹是先右再左而已
08/19 12:33, 8F

08/19 12:33, , 9F
只是在左右兩邊的東西都是有順序的 所以要這樣看也是沒差
08/19 12:33, 9F

08/19 12:50, , 10F
推P幣少賺了好多XDD
08/19 12:50, 10F

08/19 12:51, , 11F
感恩~~ ^^ 之前都沒背這個解釋的法則…吃了不少苦頭 囧
08/19 12:51, 11F

08/19 18:23, , 12F
這篇要m起來!
08/19 18:23, 12F

08/19 19:32, , 13F
應該有結合的prior吧!?
08/19 19:32, 13F

08/19 19:33, , 14F
void search(int *arr[COL],int *p);
08/19 19:33, 14F

08/19 19:33, , 15F
void search(int (*arr)[COL],int *p);
08/19 19:33, 15F

08/19 19:33, , 16F
上面這兩個會不一樣....早上在測的時候發現的
08/19 19:33, 16F

08/19 19:34, , 17F
喔,我爽笨了~~ Orz
08/19 19:34, 17F

08/20 11:31, , 18F
g++ -S 讀一下就知道了哪裡不一樣了
08/20 11:31, 18F
文章代碼(AID): #1CR7p2Ty (C_and_CPP)
文章代碼(AID): #1CR7p2Ty (C_and_CPP)