我有一個函數sortbyName
,它返回一個遞歸數據結構sortedList
。 sortList
本身包含一個指向另一個遞歸數據結構stud_type
的指針,它們全部定義如下。爲C中的遞歸數據結構定義變量C
typedef struct stud_type_ {
int matricnum;
char name[20];
struct stud_type_ *next_student;
} stud_type;
typedef struct sort_List {
stud_type *cur;
struct sortList *next;
} sortList;
stud_type listofStudents; // Assume that it is not NULL
sortList * sortbyName() {
sortList *sList;
//sort algorithm here
return sList;
}
...
...
int main() {
//trying to define curTEST = sortbyName() here
while (curTEST!=NULL) {
printf("Name: %s\n", curTEST->cur->name);
curTEST = curTEST->next;
}
}
現在我想在main()
函數變量賦值從我sortbyName
功能保存返回值,所以我可以用while循環遍歷它,並打印出結果。那麼我如何定義這個變量?我試過sortList curTEST;
和sortList * curTEST;
無濟於事。或者我的sortbyName
函數的定義有問題嗎?
編輯: 我試着編譯它並糾正最瑣碎和不那麼瑣碎的錯誤/警告,直到它來到這個電流誤差報告,其中並沒有什麼太大的意義了我。
u2_4.c:208:15: warning: implicit declaration of function 'sortbyName' is invalid in C99
[-Wimplicit-function-declaration]
curTEST = sortbyName();
^
u2_4.c:208:13: warning: incompatible integer to pointer conversion assigning to 'sortList *'
(aka 'struct sort_List *') from 'int' [-Wint-conversion]
curTEST = sortbyName();
^~~~~~~~~~~~~~~~~~~
2 warnings generated.
Undefined symbols for architecture x86_64:
"_sortbyName", referenced from:
_main in u2_4-Szd3la.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [u2_4] Error 1
在我main
功能,我定義curTEST
這樣的:sortList * curTEST;
再次讀取您的類型名稱。 'sortList * curTest = ...'。並且在'struct sort_List * next;'的定義中,您需要正確拼寫'struct'標籤。 – 2013-05-04 12:23:48
'sortedList' - >不應該是'sortList'嗎? – stijn 2013-05-04 12:24:00
'sortList *'not'sortedList *' – suspectus 2013-05-04 12:25:23