I'm trying to use printf to print out an array of chars inside a pointer called point_person, When I'm using it to return the first name of an user it prints out a lot of garbage and then finally the name.
The code looks kind of like this:
person dequeue_person;
person *point_person = &dequeue_person;
get_person(point_person, 9);
printf("%s", point_person->first_name);
person is a typedef struct containing 3 char variables: first_name[64], last_name[64], pes_nbr[64].
The output looks like this:
F÷rnamn: John ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠John
What causes this and how do I avoid this in the future?
EDIT: The code populating first_name looks like this:
void get_person(person *pers, int index)
{
person per_son;
strcpy(per_son.first_name, queue[(index+head)%QUEUE_MAX_SIZE].first_name);
strcpy(per_son.last_name, queue[(index+head)%QUEUE_MAX_SIZE].last_name);
strcpy(per_son.pers_nbr, queue[(index+head)%QUEUE_MAX_SIZE].pers_nbr);
pers = &per_son;
printf("Förnamn: %s\n", per_son.first_name);
}
EDIT 2: Figured it out by myself. I had to edit the get_person function and remove the per_son struct and change the strcpy to pers instead of pers_son.