Given the struct below, I am creating a function that takes in a person_in_queue
and a position_num
and allocating a new queue_t
struct that is added to the end of a list of queue_t
structs as specified by the first argument.
typedef struct queue {
int position_num;
char *person_in_queue;
struct queue *next_in_line;
} queue_t;
I have written my code as such:
queue_t *add_to_queue(queue_t *input_queue, char *person_in_queue, int position_num) {
input_queue = malloc(sizeof(queue_t));
assert(input_queue != NULL);
input_queue->position_num = position_num;
input_queue->person_in_queue = (char *) malloc((strlen(new_text) + 1) * sizeof(char));
assert(input_queue->person_in_queue != NULL);
strcpy(input_queue->person_in_queue, person_in_queue);
return input_queue;
}
Said code compiles, however, I am told that my code fails given that less memory is allocated than what is expected. At the moment, I'm not sure where I am going wrong here. Do note that I need to use malloc()
!
Many thanks!