Created
April 15, 2016 05:27
-
-
Save shichao-an/4337a3f6b19adb668086543bb9699ee9 to your computer and use it in GitHub Desktop.
Get offsets of struct members using offsetof
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
#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