Skip to content

Instantly share code, notes, and snippets.

@justinmeiners
Created October 30, 2018 03:28
Show Gist options
  • Save justinmeiners/76681e17df385920133b858f7cf89cf3 to your computer and use it in GitHub Desktop.
Save justinmeiners/76681e17df385920133b858f7cf89cf3 to your computer and use it in GitHub Desktop.
How to copy a struct into a byte buffer.
// how to write a struct to a buffer.
typedef struct {
int version;
int count;
char id[8];
} FileInfo;
FileInfo info;
// start out with some space
char buffer[1024];
size_t size = 0;
memcpy(buffer + size, &info.version, sizeof(int));
size += sizeof(int);
memcpy(buffer + size, &info.count, sizeof(int));
size += sizeof(int);
memcpy(buffer + size, info.id, sizeof(char) * 8);
size += sizeof(char) * 8;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment