i want to create queue with dynamic array, the problem is when i test the q.rear value in int main() (for inserting data later) it’s not -1 anymore. Which part did i do wrong?
here’s a piece of the code:
#include <stdio.h>
#include <conio2.h>
#include <stdlib.h>
typedef struct{
int *data;
int front,rear,max;
}tqueue;
tqueue create(int size)
{
tqueue q;
q.data=(int *)malloc(size*sizeof(int));
q.front=0;
q.rear=-1;
q.max=size;
return q;
}
int main()
{
tqueue q;
int size=4;
create(size);
printf("\n%d",q.rear);
getch();
return 0;
}
You didn’t assign the result of
create()toq. The following callscreate()and discards the result, leavingqin its original state:To fix, replace the above line with: