0

I'm creating a queue datastructure in c.

typedef struct Queue_node{

int value;
struct Queue_Node* next;

};

struct Queue_Node* front = NULL;
struct Queue_Node* R = NULL;

void Enqueue(int x) 
{
    struct Queue_node* temp = (struct Queue_node*)malloc(sizeof(struct  Queue_node*));
    temp->value = x;
    temp->next = NULL;

    if (front == NULL && R == NULL)
    {
       R = temp;
       front = R;
       return;
    }

    R->next = temp;
    R = temp;
}

At line 24 (R->next = temp), the compiler tells me:

dereferencing pointer to incomplete type 'struct Queue_node'.

I cant access R->next after the declarations, why?

Umaiki
  • 354
  • 2
  • 14
Erik
  • 67
  • 1
  • 10
  • 2
    `typedef struct Queue_node{ ... };` -> `typedef struct Queue_node{ ... } Queue_node;` --- or just leave out the `typedef` as you don't use it. –  Apr 20 '18 at 09:48
  • 6
    Not what you ask for, but `malloc(sizeof(struct Queue_node*))` allocates space for a pointer, not for a struct. – Bo Persson Apr 20 '18 at 09:50
  • 1
    And that's why you simply write `struct Queue_node *temp = malloc(sizeof *temp);` -- clean, simple, safe. –  Apr 20 '18 at 09:51
  • 1
    The problem is that you did not `#include `. That caused the compiler (I got the same error with gcc) to get confused since `NULL` was not declared. The thing is, you reported the LAST error of several. You need to check the FIRST error message (the one furthest up the screen) not the LAST. Typically, the first few error messages are meaningful, but (their causes) confuse the compiler, and subsequent error messages can be misleading. – Peter Apr 20 '18 at 09:51
  • Since this is simply a problem with a missing header, that you should have included, I'm voting to close as a typographical error. – Peter Apr 20 '18 at 09:54
  • @Peter, I doubt it is about missing `NULL`. I assume it is missing definition of `struct Queue_Node` due to upper/lower case mismatch or typo. – Gerhardh Apr 20 '18 at 12:54
  • @Gerhardh - I copied the code "as is" and compiled with gcc. The result was a series of error messages and warnings, the last of which was the one described by the OP. After adding `#include `, the code compiled with warnings. – Peter Apr 20 '18 at 21:50
  • @Peter I compiled using GCC and after adding ` #include ` to the code I just got a few less warnings (no more complaints about `NULL` and `malloc`) but I still get errors and warnings: "test.c:30:6: error: dereferencing pointer to incomplete type R->next = temp;" – Gerhardh Apr 23 '18 at 09:12

3 Answers3

2

There are few problems in your code, firstly give alias name while doing typedef.

typedef struct Queue_node{
    int value;
    struct Queue_Node* next;
}Queue_node;/*put alias name here */

Secondly, malloc() memory allocation is not correct

 struct Queue_node* temp = (struct Queue_node*)malloc(sizeof(struct  Queue_node*));

It should be(allocate memory equal to size of Queue_node, not Queue_node* )

 struct Queue_node* temp = malloc(sizeof(struct  Queue_node)); 

Also avoid casting malloc() result. See this post Do I cast the result of malloc?

Achal
  • 11,821
  • 2
  • 15
  • 37
  • 2
    While this is correct, I'd strongly recommend `struct Queue_node *temp = malloc(sizeof *temp);` instead (drop the unnecessary cast to improve readability and use `sizeof` on an *expression*, so you can't create bugs when later changing the type) –  Apr 20 '18 at 09:57
  • yes @FelixPalmen Agree. – Achal Apr 20 '18 at 09:59
  • This does not solve the upper case/lower case mismatch of the struct tags. With this change the code will still not compile. – Gerhardh Apr 25 '18 at 10:26
1
struct Queue_node{

int value;
struct Queue_Node* next;

};

or

typedef struct Queue_Node{

int value;
struct Queue_Node* next;

}Queue_Node;

will do it.

schorsch312
  • 5,553
  • 5
  • 28
  • 57
0

While the existing answers already told you that you are missing a name for your typedef, you have a much simpler problem:

typedef struct Queue_node{
  int value;
  struct Queue_Node* next;
};

What is basically wrong is a simple typo!

You define a struct Queue_node. And inside it you want to use a pointer to struct Queue_Node which does not exist.

In C struct tags, type names or any other identifiers are case sensitive.

Change your type definition like this

struct Queue_Node{
  int value;
  struct Queue_Node* next;
};

or

typedef struct Queue_Node{
  int value;
  struct Queue_Node* next;
} Queue_Node;

You also might use Queue_node but it is important to use same way of writing it throughout the whole code.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39