Home > Blockchain >  Dereferencing pointer to incomplete type for a struct called within a struct
Dereferencing pointer to incomplete type for a struct called within a struct

Time:10-31

Looking at a lot of other similar questions, they don't seem to solve this issue of a struct being called within a struct.

I have a simple queue.c file which has several structs like struct Node, struct Queue, struct Queue* newQueue(int cap) etc...

here are the two relevant structs for this question inside the file job_queue.c:

...
struct Queue {
    int size;
    int max_size;
    struct Node *head;
    struct Node *tail;
};

struct Queue* newQueue(int capacity)
{
    struct Queue *q;
    q = malloc(sizeof(struct Queue));

    if (q == NULL) {
        return q;
    }

    q->size = 0;
    q->max_size = capacity;
    q->head = NULL;
    q->tail = NULL;

    return q;
}
...

Here is the queue.h header file which I import into my main file.

#ifndef __SIMPLE_QUEUE__
#define __SIMPLE_QUEUE__

struct Queue;

extern struct Queue*
newQueue(int capacity);

extern int
enqueue(struct Queue *q, char* value);

extern void*
dequeue(struct Queue *q);

extern void
freeQueue(struct Queue *q);

#endif

I clearly define the names for the structs as guided by the other posts. In my main C file, I initialize the queue from the following

struct Queue* job_queue = newQueue(queue_cap);

I pass it to another function with the paramaters as follows:

void queueing(struct Queue* jobs_queue){
   ...
}

Within this function, I am trying to print the max_value by doing the following:

printf("Queue size: %d", jobs_queue->max_size);

However, I get the error: dereferencing pointer to incomplete type 'struct Queue'

What do I have to fix to get rid of this error?

CodePudding user response:

Your queue.h file only declares struct Queue. It doesn't define it, so files other than queue.c don't see the definition.

You need to put the full definition of the struct in queue.h for it to be seen in other files.

  • Related