我試圖通過套接字發送一個結構,為此,我對其進行序列化和反序列化
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;
}
uj5u.com熱心網友回復:
始終將物件的地址與 memcpy 一起使用。您正在使用該值,您需要發送指標.. 例如:
memcpy(&pcb->srt,stream offset, sizeof(float));
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471939.html
