05-24-2023 10:06 AM
I have a structure as follows:
typedef struct {
long SensorId;
unsigned long PacketNo;
unsigned long RCS_Tag;
long Sensor_Echo;
unsigned short WaterPressure;
unsigned short AirPressure;
DgpsS Dgps;
unsigned short UWJB_Echo;
unsigned long Environment;
short ReservedOnline;
long IDFTag[2];
unsigned long Reserved; //Last_Packet;
} Packet
and Character buffer of size 16448: char* Buff
when I try to do the following I get a Arguement too small error:
memcpy((char *)&(Packet.PacketNo),Buffer[16388],52);
What I am trying to do is write 52 bytes from Buffer starting from Offset of 16388 byte to the Packet structure, but instead of writing it from the begining I do not want to overwrite the Packet.SensorID values and want to start writing from the address of Packet.PacketNo.
I am using LabWindows/CVI 2020
05-24-2023 12:18 PM
You need to add a "&" in front of Buffer[16388].
05-24-2023 11:37 PM
Even after doing,
memcpy((char *)&(Packet.PacketNo),&Buffer[16388],52);
Still receiving the same error.
05-25-2023 01:07 AM
Turns out I cannot write Structures like arrays assuming that if I write more bytes it will just write to the next element.
Since Packet.PacketNo. is of type unsgined long I can only write 4 bytes to it, where I was trying to write a stream of 52bytes.
changing the number of bytes to be written solves the problem.
However, please let know if there is better way of achieving what I was trying to do i.e. copy a stream of data to a given section of a structure.
05-30-2023 03:16 AM
check the size of your structure. Structures are not always packed by single bytes and even a short might use 4 Bytes in a structure. You can force a structure by using #pragma pack(1), do stay at smallest size.
Also you need to check if the first entry of the structure is also the start adress of the struct. I'm not sure about the memory alignement. Also possible that the last entry is the start adress.