I am making a message queue API in C, but I am having a problem with the receive method. I send a char* message (“BOB”) to the message queue and it is successfully stored. I then try to receive the message but that fails.
Inside mq_receive() the correct message is dequeued and ret_val->buf points to 0x012f5754 (“BOB”). Next, msg_ptr (which is originally 0x00000000) is assigned 0x012f5754. Everything works as expected until the program returns to main(). In main(), receive_message is still NULL. I was expecting it to point to the first character of BOB which is 0x012f5754. What am I doing wrong? Thanks.
//main.c
main(){
char* receive_message = NULL;
//message queue init ...
mq_send(msq_id, "BOB"); //this works correctly
mq_receive(msq_id, receive_message);
printf("return value: %p\n", receive_message);
}
//message_queue.c
mqd_t mq_receive(mqd_t mqdes, char *msg_ptr)
{
queue_t* ret_val;
q_attr* attr_ptr = (q_attr*)mqdes;
ret_val = dequeue(attr_ptr);
//all this works ret_val->buf points to BOB
msg_ptr = ret_val->buf;
return mqdes;
}
Arguments are passed by value in C, you need to pass the address of
receive_messagetomq_receive():and invoke it: