char* createMSG(uint8_t i,uint16_t port) {
char *buff;
buff = (char*) calloc(1,6);
uint8_t id, tmp;
tmp = 0;
id = 2;
memcpy(buff, &id, sizeof(uint8_t));
memcpy(buff+1, &i, sizeof(uint8_t));
memcpy(buff+2, &port, sizeof(uint16_t));
memcpy(buff+2+2, &tmp, sizeof(uint16_t));
memcpy(buff+2+2+1, &tmp, sizeof(uint16_t));
printf("created SV_CON_REP: id: %d accept: %d port %d\n",*buff,*(buff+1),* (buff+2)); return buff;
}
我需要将端口复制到 uint32_t 中。它打印端口为空。
编辑 函数调用:char* tmp; uint8_t 我;我 = 9; uint16_t 端口;端口 = 1234;tmp = createMSG(i,port);
输出:创建的味精:id:2 接受:0 端口 0