RicardasSim

offsetof

Jan 27th, 2020
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. //-std=c99 -Wall -Wextra -Wpedantic -Wshadow
  2. //gcc 64bit
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stddef.h>
  7.  
  8. int main()
  9. {
  10.  
  11.     typedef float vec2[2];
  12.     typedef float vec3[3];
  13.     typedef float vec4[4];
  14.  
  15.     struct st{
  16.         char a;
  17.         int b;
  18.         float c;
  19.         double d;
  20.         vec2 v2;
  21.         vec3 v3;
  22.         vec4 v4;
  23.     };
  24.  
  25.     printf( "offsetof  a: %ld\n", offsetof( struct st, a) );
  26.     printf( "offsetof  b: %ld\n", offsetof( struct st, b) );
  27.     printf( "offsetof  c: %ld\n", offsetof( struct st, c) );
  28.     printf( "offsetof  d: %ld\n", offsetof( struct st, d) );
  29.     printf( "offsetof v2: %ld\n", offsetof( struct st, v2) );
  30.     printf( "offsetof v3: %ld\n", offsetof( struct st, v3) );
  31.     printf( "offsetof v4: %ld\n", offsetof( struct st, v4) );
  32.  
  33.     return 0;
  34. }
  35.  
  36. /*
  37. output:
  38.  
  39. offsetof  a: 0
  40. offsetof  b: 4
  41. offsetof  c: 8
  42. offsetof  d: 16
  43. offsetof v2: 24
  44. offsetof v3: 32
  45. offsetof v4: 44
  46.  
  47. */
Advertisement
Add Comment
Please, Sign In to add comment