[問題]用 array, struct, 和 qsort 做排序

看板C_and_CPP作者 (linshihhua)時間6年前 (2017/09/11 22:52), 編輯推噓1(109)
留言10則, 5人參與, 最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) Win 7 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) Dev-C++ 5.11 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 跟小弟之前問用 qsort 排序二維陣列類似, 只是這次換成排序 array 和 struct 想宣告一個 array, 元素都是 struct, 然後去做排序 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): prog.c: In function ‘compw’: prog.c:7:12: error: dereferencing pointer to incomplete type ‘struct data’ return (p1->x) - (p2->x); ^~ prog.c: In function ‘comph’: prog.c:13:12: error: dereferencing pointer to incomplete type ‘struct data’ return (p1->y) - (p2->y); 程式碼(Code):(請善用置底文網頁, 記得排版) https://ideone.com/QjGEGU 補充說明(Supplement): 看不懂哪裡出問題, 麻煩大家幫忙解惑. 感恩 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.224.163.247 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1505141531.A.BCF.html

09/11 22:58, , 1F
compw 和 comph 看不到 struct data 的定義
09/11 22:58, 1F

09/11 22:59, , 2F
把 struct data 的定義從 main 裡移到 compw() 之前
09/11 22:59, 2F

09/11 23:09, , 3F
謝謝, 想請問我試著在前面宣告函數的原型
09/11 23:09, 3F

09/11 23:10, , 4F
然後把函數搬到 main 的後面也會有問題
09/11 23:10, 4F

09/11 23:10, , 5F
這是因為 struct 只有在 main 函數裡有定義
09/11 23:10, 5F

09/11 23:11, , 6F
而離開 main 函數之後就成了未定義的關係嗎
09/11 23:11, 6F

09/11 23:52, , 7F
在大括弧內宣告的話在大括弧外是看不到的
09/11 23:52, 7F

09/11 23:53, , 8F
應該是要把 struct 定義拿出去而不是把 implementation
09/11 23:53, 8F

09/11 23:53, , 9F
往後移
09/11 23:53, 9F

09/11 23:54, , 10F
通常 struct 的宣告都會在外面或是 header 裡面
09/11 23:54, 10F
文章代碼(AID): #1PjgCRlF (C_and_CPP)