我有一个sortbyName
返回递归数据结构的函数sortedList
。sortList
它本身包含一个指向另一个递归数据结构的指针stud_type
,它们都定义如下。
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;