看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《defsrisars (阿轉)》之銘言: : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : DevC 5.8.2 : 問題(Question): : 小的最近初學C語言 : 目前大一在做C語言實習課的期末作業 : 但有些問題上網看了許久還是找不太到答案QQ : (像extern 結構避免重複宣告也是上網看了很久才知道) : 希望有大大能夠不吝指導一下 : 1. void Deal_First(int *card,Player *P,int *flag); : 這一行會跑出編譯訊息 : [Note] expected 'int *' but argument is of type 'int (*)[52]' : 想請問是什麼意思呢? int * 跟 int(*) 是差在哪QQ : 我呼叫的地方是這樣寫的 : Deal_First(&card,A,&flag); : 其中card跟flag是宣告int : A是用malloc做出指向結構Player(我有typedef了)的指標 : struct player * A = (struct player*)malloc(sizeof(struct player)); 你要傳一個陣列進去, 直接丟那個陣列的名字進去即可 &card 是指向 card 這個陣列的指標 (這也是 int (*)[52] 的意思:指向一個長 52 的 int 陣列的指標) : 2. 然而最主要的錯誤是出在 : 14 34 C:\Users\PC\Desktop\期末專題Dev\Deal_First.c [Error] : 'flower_table' undeclared (first use in this function) : 14 64 C:\Users\PC\Desktop\期末專題Dev\Deal_First.c [Error] : 'number_table' undeclared (first use in this function) : 也就是下方程式碼的78行 : 我看起來的認知是compiler覺得我的flower_table跟number_table沒有宣告 : 但是我5~8行宣告不是應該在main.c裡面宣告為全域變數了嗎? : (我專案應該開的沒問題~都有連結到,這行//就可以執行了) : 另外就是想請問有沒有辦法把這個5~8行放進標頭檔(全域變數放進標頭檔?) : 然後又像extern結構的用法一樣不要重複宣告呢? : 然後如果有哪裡是很爛的寫法該做修改請告訴我謝謝QQ : 謝謝大家QQ : p.s 程式碼只放部份 main.c 標頭檔H.h 跟出問題的函式 : 預期的正確結果(Expected Output): : 程式能正常運行 : 程式碼(Code):(請善用置底文網頁, 記得排版) : http://codepad.org/B41FXz3N 你的主要問題是 extern 的使用 這裡你需要的是將一個在 main.c 裡定義的東西開給 Deal_First.c 來用 因此 Deal_First.c 裡需要宣告「有這兩個陣列會定義在別處」 這便是靠 extern 宣告來達成 在此即 extern const char flower_table[4]; extern const char number_table[13][BUF_SIZE]; (相對的, struct 宣告反而不用 extern, 因為它沒有實體定義 只需要寫在 H.h 裡面, 所有人就都知道了) 由於每個使用到的地方都需要這個宣告 因此這兩行 extern 便需要擺在 H.h 裡面 至於 main.c 裡的那兩行則必須留著, 那就是上面講的「定義在別處」的「別處」 這個「別處」會在編譯完之後的連結階段才將它們湊在一起 如此 Deal_First.c 就能使用到這個陣列了 -- 'You've sort of made up for it tonight,' said Harry. 'Getting the sword. Finishing the Horcrux. Saving my life.' 'That makes me sound a lot cooler then I was,' Ron mumbled. 'Stuff like that always sounds cooler then it really was,' said Harry. 'I've been trying to tell you that for years.' -- Harry Potter and the Deathly Hollows, P.308 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.30.32 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1418967733.A.7B2.html
defsrisars: 謝謝!!! 非常簡單明瞭~~我懂囉!!謝謝您~ 12/19 13:46
defsrisars: 我在po文發問有個地方說錯也被您發現了 12/19 13:49
defsrisars: 我那邊傳進去的&card不是int 是int[] QAQ 12/19 13:50
CCWck: 你的程式OK嗎? 12/19 14:20
defsrisars: 就剛剛發問的地方OK 不過我又遇到新的問題了QQ 12/19 18:50