This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

AM3359 data array modification

Folks,

I have a one_byte_array[32], and I have 2_bytes_struct{element1, element2, element3, element4}

I am trying to copy the first 10 bytes of one_byte_array into 2_bytes_struct.

I used this, but its not working properly: memcpy(&(2_bytes_struct),one_byte_array,11);

I believe is a formatting issue that I'm having in the data after I pass it. I get a value of zero for each element in 2_bytes_struct after the copying is done. And when I print one_byte_array I can see its data.

one_bye_array is has the first 11 elements (The ones I want) stored in the following order:

{value1, value2 lsb, value2 msb, value3 lsb, value3 msb, value4 lsb, value4 msb, value5 lsb, value5 msb, value6, value7}

For instance, value2 lsb and value2 msb shall be stored in element1 of the struct (since element1is two bytes), and so forth.

How can I fix and store the data correctly? Any bit shifting that needs to be done here?

Actual code:

typedef struct
{
    UINT8 element1;
    INT16 element2;
    INT16 element3;
    INT16 element4;
    INT16 element5;
    INT16 element6;
} 2_bytes_struct;

UINT8  one_byte_array[32];

memcpy(&2_bytes_struct, one_byte_array, 11);
  • You don't account for alignment within the structure.  You presume the structure is packed, with no gaps between elements.  That is not the case.  Due to alignment, element2 begins at offset 2, not 1.  All of the INT16 elements are aligned on 2 byte boundaries.  There is a one byte gap between element1 and element2.  See here for further explanation.

    Pototo Clark said:
    memcpy(&2_bytes_struct, one_byte_array, 11);

    Rather than 11, that last argument should be sizeof(2_bytes_struct).  This alone does not fix your problem.  But it makes the code easier to maintain.

    Actually, there are a few problems in your code example.  I presume you typed it into the forum entry manually, and did not check whether it compiles.  That is not a problem at all.  I took your example to be conceptually correct, and easily ignored the minor errors in syntax.

    Thanks and regards,

    -George