typedef - Defining variable for recursive data structure in C -
i have function sortbyname
returns recursive data structure sortedlist
. sortlist
contains pointer recursive data structure stud_type
, defined below.
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 not null sortlist * sortbyname() { sortlist *slist; //sort algorithm here return slist; } ... ... int main() { //trying define curtest = sortbyname() here while (curtest!=null) { printf("name: %s\n", curtest->cur->name); curtest = curtest->next; } }
now assign variable in main()
function hold return value sortbyname
function can iterate through while loop , print out results. how define variable? tried sortlist curtest;
, sortlist * curtest;
no avail. or there wrong definition of sortbyname
function?
edit: i've tried compiling , corrected of trivial , not trivial errors/warnings until came current error report doesn't make sense me.
u2_4.c:208:15: warning: implicit declaration of function 'sortbyname' invalid in c99 [-wimplicit-function-declaration] curtest = sortbyname(); ^ u2_4.c:208:13: warning: incompatible integer pointer conversion assigning 'sortlist *' (aka 'struct sort_list *') 'int' [-wint-conversion] curtest = sortbyname(); ^ ~~~~~~~~~~~~~~~~~~ 2 warnings generated. undefined symbols architecture x86_64: "_sortbyname", referenced from: _main in u2_4-szd3la.o ld: symbol(s) not found architecture x86_64 clang: error: linker command failed exit code 1 (use -v see invocation) make: *** [u2_4] error 1
in main
function, defined curtest
this: sortlist * curtest;
are defining function in different file... or after define main()? if defined elsewhere , not have function prototype before main(), warnings , linker error.
i did following in single file (main.c) , compiled without issues.
typedef struct stud_type_ { int matricnum; char name[20]; struct stud_type_ *next_student; } stud_type; typedef struct sort_list { stud_type *cur; struct sort_list *next; } sortlist; stud_type listofstudents; // assume not null sortlist * sortbyname() { sortlist *slist; //sort algorithm here return slist; } int main() { sortlist * curtest = sortbyname(); while (curtest!=null) { printf("name: %s\n", curtest->cur->name); curtest = curtest->next; } return 0; }
note made 2 changes file. changed structure sortlist when defining pointer next. changed struct sortlist *next
struct sort_list *next
. , defined , initialized curtest sortlist * curtest = sortbyname()
.
Comments
Post a Comment