C: Incomplete return type
I tried to implement a linked list in C and started with this simple code:
#include <stdio.h>
struct int_node;
struct int_list;
struct int_list create_list();
int main() {
struct int_list list = create_list();
return 0;
}
struct int_node {
int value;
struct int_node * next;
};
struct int_list {
struct int_node * first;
};
struct int_list create_list() {
/* Creates an empty list, first node=null */
struct int_list list;
list.first = NULL;
return list;
};
I got two errors when building:
- Call 'create_list' with incomplete return type 'struct int_list'
- The variable is of incomplete type 'struct int_list'
I was looking for an answer and all I could find was that I need to declare structures and functions before using them, which is what I did.
Another thing I tried was to move the main () function to the end and resolve the errors, but this is a stupid workaround and I want to find a real solution.
Any help?
source to share
You actually need to define structs before using them, otherwise the compiler knows what it struct int_node
is when it sees it in the main method. Since Bill Lynch said your simple fix is ββactually the right way.
You should look at exactly how the program goes from source code to executable in C if you are interested in learning more. Here's an interesting and short page about it.
source to share