Converting struct to byte and back to struct

前端 未结 4 1590
刺人心
刺人心 2021-01-31 04:21

I\'m currently working with Arduino Unos, 9DOFs, and XBees, and I was trying to create a struct that could be sent over serial, byte by byte, and then re-constructed into a stru

4条回答
  •  广开言路
    2021-01-31 04:43

    You do things in the wrong order, the expression

    &struct_data+i
    

    takes the address of struct_data and increases it by i times the size of the structure.

    Try this instead:

    *((char *) &struct_data + i)
    

    This converts the address of struct_data to a char * and then adds the index, and then uses the dereference operator (unary *) to get the "char" at that address.

提交回复
热议问题