Reputation: 877
I have a function sortbyName
that returns a recursive data structure sortedList
. sortList
itself contains a pointer to another recursive data structure stud_type
and they are all defined as 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 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;
}
}
Now I would like to assign a variable in the main()
function to hold the return value from my sortbyName
function so I can iterate through it with a while loop and print out the results. How do I then define this variable? I tried sortList curTEST;
and sortList * curTEST;
to no avail. Or is there something wrong with my definition of the sortbyName
function?
EDIT: I've tried compiling it and corrected most of the trivial and not so trivial errors/warnings until it came to this current error report which doesn't make too much sense to me.
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
In my main
function, I defined curTEST
like this: sortList * curTEST;
Upvotes: 0
Views: 1504
Reputation: 1977
Are you defining your function in a different file... or after you define main()? If it is defined elsewhere and you do not have a function prototype before main(), then you will get the warnings and linker error.
I did the following all in a single file (main.c) and it compiled without any 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 that it is 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 that I only made two changes to your file. I changed your structure for sortList when defining the pointer to next. I changed it from struct sortList *next
to struct sort_List *next
. And I defined and initialized curTEST as sortList * curTEST = sortbyName()
.
Upvotes: 1