[ create a new paste ] login | about

Link: http://codepad.org/wE8dbBV1    [ raw code | output | fork ]

C, pasted on Dec 13:
#include <stdio.h>
#include <stdint.h>

typedef struct Device dev;
struct Device {
  uint32_t address;
  uint32_t id;
};

int main(void) {
    //create an instance `sample.address=123` and `sample.id=456`
    dev sample = (dev) { 123, 456 };
    //convert dev pointer to byte pointer, so you loop through bytes
    uint8_t* b_sample = (uint8_t *)(&sample);
    //buffer for copy
    uint8_t* bytes[1024];

    int size = (int)(sizeof(dev)/sizeof(uint8_t)), i;
    for(i = 0; i < size; i++) {
        bytes[i] = b_sample[i];
        //see what values you copy
        printf("%x ", bytes[i]);
    }

    return 0;
}


Output:
1
7b 0 0 0 c8 1 0 0 


Create a new paste based on this one


Comments: