memcpy无法从流到浮点变量复制
我试图通过套接字发送结构,为此,
typedef struct pcb{
uint16_t id;
uint16_t tamano;
uint8_t pc;
int tp;
float srt;
}t_pcb;
在尝试重建结构时,我的问题序列化和应对我的问题,特别是在尝试存储浮点值时。
static void deserializar_header_PCB_KTC(void* stream, t_pcb* pcb, uint8_t *tamanoInstrucciones) {
int offset = 0;
memcpy(pcb->id, stream, sizeof(uint16_t));
offset = sizeof(uint16_t);
memcpy(pcb->tamano, stream+offset, sizeof(uint16_t));
offset += sizeof(uint16_t);
memcpy(pcb->pc, stream+offset, sizeof(uint8_t));
offset += sizeof(uint8_t);
memcpy(pcb->tp, stream+offset, sizeof(int));
offset += sizeof(int);
memcpy(pcb->srt,stream+offset, sizeof(float));
offset += sizeof(float);
memcpy(tamanoInstrucciones, stream+offset, sizeof(int));
}
出现以下错误:
src/protocolo.c:323:12: error: incompatible type for argument 1 of ‘memcpy’
memcpy(f, stream+offset, sizeof(float));
我已经使用int类型而不是float测试了通信,并且可以正常工作。我考虑过乘以浮点值并使用int类型,但是我会失去定义,因此我试图找到其他方式。
如果有人能帮助我,我真的很感激。
序列化
static void* serializar_header_PCB_KTC(t_pcb* pcb) {
void* stream = malloc(sizeof(op) + sizeof(int) + sizeof(uint16_t) * 2 + sizeof(uint8_t) * 2 + sizeof(float));
void* pos = stream;
uint8_t tamanoInstrucciones = tamano_intrucciones_pcb(pcb);
op cop = HEADER;
memcpy(pos, &cop, sizeof(op));
pos += sizeof(op);
memcpy(pos, &pcb->id, sizeof(uint16_t));
pos += sizeof(uint16_t);
memcpy(pos, &pcb->tamano, sizeof(uint16_t));
pos += sizeof(uint16_t);
memcpy(pos, &pcb->pc, sizeof(uint8_t));
pos += sizeof(uint8_t);
memcpy(pos, &pcb->tp, sizeof(int));
pos += sizeof(int);
memcpy(pos, &pcb->srt, sizeof(float));
pos += sizeof(float);
memcpy(pos, &tamanoInstrucciones, sizeof(uint8_t));
return stream;
}
Im trying to send a struct through a socket, for this, im serializing and deserializing it
typedef struct pcb{
uint16_t id;
uint16_t tamano;
uint8_t pc;
int tp;
float srt;
}t_pcb;
My problem comes when trying to rebuild the struct, specifically when trying to store the float value.
static void deserializar_header_PCB_KTC(void* stream, t_pcb* pcb, uint8_t *tamanoInstrucciones) {
int offset = 0;
memcpy(pcb->id, stream, sizeof(uint16_t));
offset = sizeof(uint16_t);
memcpy(pcb->tamano, stream+offset, sizeof(uint16_t));
offset += sizeof(uint16_t);
memcpy(pcb->pc, stream+offset, sizeof(uint8_t));
offset += sizeof(uint8_t);
memcpy(pcb->tp, stream+offset, sizeof(int));
offset += sizeof(int);
memcpy(pcb->srt,stream+offset, sizeof(float));
offset += sizeof(float);
memcpy(tamanoInstrucciones, stream+offset, sizeof(int));
}
the following error comes up:
src/protocolo.c:323:12: error: incompatible type for argument 1 of ‘memcpy’
memcpy(f, stream+offset, sizeof(float));
i already tested the communication using int type instead of float and it works properly. I considered multiplying the float value and use int type, but i would lose definition so im trying to find some other way.
if anyone could help me with this i would really appreciate it.
Serialization
static void* serializar_header_PCB_KTC(t_pcb* pcb) {
void* stream = malloc(sizeof(op) + sizeof(int) + sizeof(uint16_t) * 2 + sizeof(uint8_t) * 2 + sizeof(float));
void* pos = stream;
uint8_t tamanoInstrucciones = tamano_intrucciones_pcb(pcb);
op cop = HEADER;
memcpy(pos, &cop, sizeof(op));
pos += sizeof(op);
memcpy(pos, &pcb->id, sizeof(uint16_t));
pos += sizeof(uint16_t);
memcpy(pos, &pcb->tamano, sizeof(uint16_t));
pos += sizeof(uint16_t);
memcpy(pos, &pcb->pc, sizeof(uint8_t));
pos += sizeof(uint8_t);
memcpy(pos, &pcb->tp, sizeof(int));
pos += sizeof(int);
memcpy(pos, &pcb->srt, sizeof(float));
pos += sizeof(float);
memcpy(pos, &tamanoInstrucciones, sizeof(uint8_t));
return stream;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
始终使用Memcpy的对象的地址。您正在使用该值,需要发送指针。例如:
Always use the adresses of the objects with memcpy. You are using the value and you need to send the pointer.. for example: