C + Queue + Pointer Problem

Status
Not open for further replies.
Hello All,

I am attempting to build a queue ADT using Linked Lists in C. The following is the relevant code fragment...


typedef struct QueueNodeTag
{
char data[MAXLEN];
struct QueueNodeTag * next;
} QueueNode;

typedef struct
{
struct QueueNode *head;
struct QueueNode *tail;
} Queue;
...
...
void Insert (Queue *q, char *to_be_sent)
{
QueueNode *temp;
...
>>> q -> head = temp; // Special case for the first
>>> q -> tail = temp; // element in the Linked List
}


The problem is with the statements marked >>> At this point the compiler returns a:
"warning: assignment from incompatible pointer type".

I don't understand, q->head is of type QueueNode, and temp is also of type QueueNode...

All your help is appreciated. Thanks.

Regards,
Hellfire
:D Constantly working on "it" :D

[This message was edited by hellfire on January 20, 2002 at 23:09.]
 

bugbum1

Ars Scholae Palatinae
893
the code you included compiles without that warning on my system. i did have to assign temp=0; to get rid of uninitialized variable warning....

code:<HR>
Code:
#define MAXLEN 10<P>typedef struct QueueNodeTag
{
char data[MAXLEN];
struct QueueNodeTag * next;
} QueueNode;<P>typedef struct 
{
struct QueueNode *head;
struct QueueNode *tail;
} Queue;<P>void Insert (Queue *q, char *to_be_sent)
{
QueueNode *temp;<P>temp = 0;<P>	q->head = temp; // Special case for the first
	q->tail = temp; // element in the Linked List
}

if you e-mail me the whole file, i'll compile it to confirm, but your snipet compiles fine (also turned warning level to max to catch other things).
 

bugbum1

Ars Scholae Palatinae
893
oh, geez. i just saw the problem... sorry. i was using .cpp which let me get away with the problem - C .c is picky...

i see this error with .c file

tst.c(24) : warning C4133: '=' : incompatible types - from 'struct QueueNodeTag
*' to 'struct QueueNode *'
tst.c(25) : warning C4133: '=' : incompatible types - from 'struct QueueNodeTag
*' to 'struct QueueNode *'
 

bugbum1

Ars Scholae Palatinae
893
this seems to work... sorry about the original post (was using tst.cpp and it didn't catch the problem).


code:<HR>
Code:
#define MAXLEN 10<P>typedef struct QueueNodeTag
{
char data[MAXLEN];
struct QueueNodeTag * next;
} QueueNode;<P>typedef struct 
{
struct QueueNode *head;
struct QueueNode *tail;
} Queue;<P>void Insert (Queue *q, char *to_be_sent)
{
struct QueueNode *temp;<P>temp = 0;<P>q -> head = temp; // Special case for the first
q -> tail = temp; // element in the Linked List
}
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
Originally posted by bugbum1:
in the original code you had

QueueNode *temp;
instead of
struct QueueNode *temp;

Shouldn't it be the other way around?

Since he typedef'd "struct QueueNodeTag" as "QueueNode", he should never use "struct QueueNode". Otherwise what would be the point of using typedef?

I think the problem is in his definition of the Queue struct, where he should have "QueueNode*" instead of "struct QueueNode*" for the head and tail fields.
 
Status
Not open for further replies.