Skip to content

Instantly share code, notes, and snippets.

@shichao-an
Created April 15, 2016 05:27
Show Gist options
  • Save shichao-an/4337a3f6b19adb668086543bb9699ee9 to your computer and use it in GitHub Desktop.
Save shichao-an/4337a3f6b19adb668086543bb9699ee9 to your computer and use it in GitHub Desktop.
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