I’m trying to enqueue a struct array but it crashes whenever I try to insert information into it.
The struct itself looks like this:
typedef struct {
char first_name;
char last_name;
char pers_nbr;
} person;
While the function looks like this:
void enqueue(person pers)
{
strcpy(queue[tail].first_name, pers.first_name);
strcpy(queue[tail].last_name, pers.last_name);
strcpy(queue[tail].pers_nbr, pers.pers_nbr);
tail = (tail+1) % QUEUE_MAX_SIZE;
nbr_elem++;
}
I’ve been debugging and it crashes at the first strcpy and returns the error “Access violation reading location”. The issue seems to be with the pers object.
Here’s how I’ve been trying to insert data:
person test;
test.first_name = "John";
test.last_name = "Doe";
test.pers_nbr = "A";
enqueue(test);
What causes this crash and how can I prevent it?
When you declare a variable like
it means there is space for one character
you should instead use an array of characters to hold the string
and
is not the correct way to initialize a string or a struct, either do it in the declaration or manually strcpy in the string as u did previously: