send struct in mq_send

非 Y 不嫁゛ 提交于 2019-11-29 13:36:31

You just need to pass the address of the struct and cast it to the appropriate pointer type: const char * for mq_send and char * for mq_receive.

typedef struct Req
{
  pid_t pid;
  char data[4096];
} Req;

Req buf;

n = mq_receive(mqdes0, (char *) &buf, sizeof(buf), NULL);

mq_send(mqdes1, (const char *) &buf, sizeof(buf), 0);

You can use memcpy as follows:

char * data; //Do appropriate allocation.

memcpy(data, &req, sizeof(req)));

On receiving you copy back received data into struct.

memcpy(&rec, data, sizeof(rec)));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!