[问题]用 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