Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get offsets of struct members using offsetof
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int x;
short y[3];
long long z;
} data_t;
int main(int argc, char* argv[])
{
printf("x %d\n", offsetof(data_t, x));
printf("y %d\n", offsetof(data_t, y));
printf("y[1] %d\n", offsetof(data_t, y[1]));
printf("z %d\n", offsetof(data_t, z));
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment