Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Incomplete return type

I tried to implement 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;
};

While building I got two errors:

  1. Calling 'create_list' with incomplete return type 'struct int_list'
  2. Variable has incomplete type 'struct int_list'

I looked for an answer and all I could find was that I need to declare the structs and the functions before I use them, which I did.

Another thing I tried was to move the main() function to the end, and that solved the errors, but it's a silly workaround and I want to find out the real solution.

Any help?

like image 802
Matan Avatar asked Dec 14 '25 05:12

Matan


2 Answers

The definitions of the structs should also be placed before main(). That is, your silly fix is actually the correct fix.

like image 112
Bill Lynch Avatar answered Dec 16 '25 21:12

Bill Lynch


Actually you need to define the structs before you use them, or else how does the compiler know what a struct int_node is when it sees it in the main method. So as Bill Lynch said your simple fix is actually just the correct way.

You should look in to how exactly a program goes from source code to an executable in C if you are interested in learning more. Here is an interesting and not to long page about it.

like image 36
Spaceman Spiff Avatar answered Dec 16 '25 22:12

Spaceman Spiff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!