When using msgsnd the structure mentioned in man page is
struct mymsg {
long mtype; /* message type */
char mtext[1]; /* body of message */
};
But if you use it like
func(char *array, int sizeofarray)
{
struct mymsg {
long mtype; /* message type */
char *ptr; /* body of message */
};
msgq.mtype = 1;
msgq.ptr = array;
msgsnd(msqid, &msgq, sizeofarray, 0);
}
Assign ptr to some local array[200] (array could be got as a parameter in function), the message received on the other side is junk. Why is this?
It’s junk because the structure you have specified is not of the form that
msgsndwants. It expects the data to be copied to immediately follow themtypevalue in memory, not be in an array somewhere else.If you want to send an array of length 200, you need to do something like: