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

作者: linshihhua (linshihhua)   2017-09-11 22:52:06
開發平台(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):
看不懂哪裡出問題, 麻煩大家幫忙解惑. 感恩
作者: stucode   2017-09-11 22:58:00
compw 和 comph 看不到 struct data 的定義
作者: Schottky (順風相送)   2017-09-11 22:59:00
把 struct data 的定義從 main 裡移到 compw() 之前
作者: linshihhua (linshihhua)   2017-09-11 23:09:00
謝謝, 想請問我試著在前面宣告函數的原型然後把函數搬到 main 的後面也會有問題這是因為 struct 只有在 main 函數裡有定義而離開 main 函數之後就成了未定義的關係嗎
作者: Lipraxde (Lipraxde)   2017-09-11 23:52:00
在大括弧內宣告的話在大括弧外是看不到的
作者: steve1012 (steve)   2017-09-11 23:53:00
應該是要把 struct 定義拿出去而不是把 implementation往後移通常 struct 的宣告都會在外面或是 header 裡面

Links booklink

Contact Us: admin [ a t ] ucptt.com