Hi there,

I am trying to write a queue ADT using a linked list in C and have hit a snag initialising my queue...

typedef struct qNode{
	int element;		
	struct qNode *next;
} qNode;

typedef struct Queue{
	struct qNode *front;			
	int len;			
} Queue;


Queue create_queue()
{
	Queue q = (Queue)malloc(sizeof(Queue));
	q->head = NULL;
	q->len = 0;
	return q;
}

I get an error "conversion to non scalar type requested", is this something to do with my struct? or does a structure need certain code to allocate memory to it?

Thanks,

Logi.

malloc always returns starting address of the memory allocated so may be the line:

Queue q = (Queue)malloc(sizeof(Queue));

could well be replaced by

Queue *q = (Queue*)malloc(sizeof(struct Queue));

>Queue q = (Queue)malloc(sizeof(Queue));
malloc always returns a pointer, so the type you're storing the result in must also be a pointer. For future reference, you don't need the cast in C (in fact, it hides potential errors). A much more convenient way to use malloc is:

/* For allocating a single object of type T */
T *p = malloc ( sizeof *p );

/* For allocating N objects of type T */
T *p = malloc ( N * sizeof *p );
This question has already been answered. Start a new discussion instead.