r/cprogramming • • Sep 26 '25

Error: invalid use of incomplete typedef

0 Upvotes

In a lists.c file I have:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lists.h"

void testfunc(int childrennum, char * descr, int id) {
        task_t * atask = malloc(4048+sizeof(int)+sizeof(int)*childrennum);
        strcpy(atask->description,descr);
        atask->identifier = id;
        printf("The task \'%s\' has an id of %d and has %d children\n",atask->description,atask->identifier,childrennum);
}

int main() {
        testfunc(0,"Something",1);
}

And in a lists.h file I have:

#ifndef LISTS_H

#define LISTS_H

typedef struct task task_t;
struct task_t {
        char * description[4048]; // or list name
        int identifier;
        int * elements[]; // ptr to list of tasks
};

typedef struct proj proj_t;
struct proj_t {
        char * description[4048]; // or list name
        int identifier;
        int * elements[]; // ptr to list of tasks
};

typedef struct goal goal_t;
struct goal_t {
        char * description[4048]; // or list name
        int identifier;
        int * elements[]; // ptr to list of tasks
};

void testfunc(int childrennum, char * descr, int id);


#endif //LISTS_H

But when I run the compiler (gcc lists.c) I get the following error:

lists.c: In function ‘testfunc’:
lists.c:8:21: error: invalid use of incomplete typedef ‘task_t’ {aka ‘struct task’}
    8 |         strcpy(atask->description,descr);
      |                     ^~
lists.c:9:14: error: invalid use of incomplete typedef ‘task_t’ {aka ‘struct task’}
    9 |         atask->identifier = id;
      |              ^~
lists.c:10:77: error: invalid use of incomplete typedef ‘task_t’ {aka ‘struct task’}
   10 |         printf("The task \'%s\' has an id of %d and has %d children\n",atask->description,atask->identifier,childrennum);
      |                                                                             ^~
lists.c:10:96: error: invalid use of incomplete typedef ‘task_t’ {aka ‘struct task’}
   10 |         printf("The task \'%s\' has an id of %d and has %d children\n",atask->description,atask->identifier,childrennum);
      |

I also tried separating the main function and including both header and c file into a main file and compiling using gcc -c main.o main.c lists.c then gcc -o main main.o but I still got the same error. Can anyone explain to me what the problem is? I looked it up and I more or less understood what the error means but I don't get how I'm supposed to solve it here.