1

So I'm having this error:

.\linkedbank.c: In function 'enqueue':
.\linkedbank.c:48:17: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
   q->rear->next = temp;

Which is weird, because im sure temp is the same exact type as q->rear->next. Am I missing something? this is the code:

#include <stdio.h>
#include <stdlib.h>

//=============STRUCTS=============//
typedef struct{
    int data;
    struct node* next;
}node;

typedef struct{
    node* front;
    node* rear;
}queue;
//============PRIMITIVE FUNCS======//
void enqueue(queue* q, int x);
int dequeue(queue* q);
//====Utility Funcs//
queue* createQueue();
void destroyQueue();
int isEmpty(queue* q); //Cannot get full! and O(1) runtime!!!
void displayQueue(queue* q);

//============ FUNCTION DEFINITIONS =====//
void enqueue(queue* q, int x)
{
    node* temp = (node*)malloc(sizeof(node));
    temp->data = x;
    temp->next = NULL;
    if(isEmpty(q))
    {
        q->front = q->rear = temp;  
    }else
    {
        q->rear->next = temp;
        q->rear = temp;
    }
}

int dequeue(queue* q)
{
    node* temp = q->front;
    if(isEmpty(q))
    {   
        fprintf(stderr, "CANNOT DEQUEUE EMPTY QUEUE");
        return -1;
    }else if(q->front == q->rear)
    {
        q->front = q->rear = NULL;
    }else
    {
        q->front = q->front->next;
    }
    return temp->data;      
    free(temp);
}

int isEmpty(queue* q){
    return (q->front->next == NULL && q->rear->next == NULL);
}

So as you can see q->rear and q->front are both (node*), and temp is also (node*) how are these incompatible if they're the same type? Im so confused, please help.

milolm
  • 11
  • 1

1 Answers1

2
typedef struct{
    int data;
    struct node* next;
}node;

This defines an anonymous struct type and defines node as a name for that type. However, you have not defined any type called struct node. The compiler thus understands it as being some opaque type, unrelated to node, that you are defining pointers to.

You probably meant to write typedef struct node { ... } node; so that the struct you've defined is called struct node and node is typedef'd as another name for that type.

For more information, see

typedef struct vs struct definitions

Why should we typedef a struct so often in C?

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82