Created
October 30, 2018 03:28
-
-
Save justinmeiners/76681e17df385920133b858f7cf89cf3 to your computer and use it in GitHub Desktop.
How to copy a struct into a byte buffer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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