Code:
struct foo
{
    int type;
    size_t length;
};

// To allocate the struct
struct foo *bar = malloc(sizeof(foo) + theBytesINeed);

// To memcpy the data out of it:
void *buffer = ...;
memcpy(buffer, bar + 1, bar->length);



The code should be self explanatory. When sending, just append the extra bytes to the size of the struct, like the call of malloc() does.


His words are wise, his face is beard.