1. Queue trouble!

Hey guys,

I am in the midsts of writing code that involves managing two queues of names, and having the ability to call one name from the queue, and then deleting it. For some reason, I can't get it to properly work. I was wondering if someone could look over these couple functions and see if you notice anything wrong with them.

Here's the two struct definitions:
Code:
```#define MAX 50
#define EMPTY 0

struct elem {
char name[MAX];
struct elem *next;
};

struct queue {
int num;
struct elem *front;
struct elem *rear;
};```
Function enqueue:
Code:
```void enqueue(queue *qu, char pass[]) {
elem *p;

strcpy(p->name, pass);
p->next=NULL;
if (!empty(qu)) {
qu->rear->next = p;
qu->rear = p;
}
else qu->front = qu->rear = p;
printf("name = %s\n", qu->rear->name);
qu->num++;
}```
Function dequeue:
Code:
```char *dequeue(queue *qu) {
char *pass = malloc(sizeof(char)*50);
elem *p;

p = qu->front;
strcpy(pass, qu->front->name);
qu->front = qu->front->next;
qu->num--;
free(p);
return (pass);
}```
I also have:
Code:
```typedef struct queue queue;
typedef struct elem elem;```
Now, what seems to happen is that when I call dequeue, the name it gives me is always the last name I add to either queue. I can't seem to get it to work.

2. No ideas?

3. Code:
```void enqueue(queue *qu, char pass[]) {
elem *p;

strcpy(p->name, pass);
p->next=NULL;
if (!empty(qu)) {
qu->rear->next = p;
qu->rear = p;
}
else qu->front = qu->rear = p;
printf("name = %s\n", qu->rear->name);
qu->num++;
}```
What does p point at - shouldn't you malloc() a new elem each time?

[I'm very surprised that this doesn't crash immediately, but I guess you may have some pointer value floating about on the stack that just happens to be valid].

--
Mats

4. Ahha, that did it. I could have sworn I had the code to alloc the memory in there before.. must have lost it in my many attempts to fix the code after the first run through. Thanks again!