RicardasSim icon

offsetof

RicardasSim | PRO | 01/27/20 10:36:48 PM UTC | 0 ⭐ | 716 👁️ | Never ⏰ | []
C |

950 B

|

None

|

0 👍

/

0 👎

//-std=c99 -Wall -Wextra -Wpedantic -Wshadow
//gcc 64bit
 
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
 
int main()
{
 
    typedef float vec2[2];
    typedef float vec3[3];
    typedef float vec4[4];
 
    struct st{
        char a;
        int b;
        float c;
        double d;
        vec2 v2;
        vec3 v3;
        vec4 v4;
    };
 
    printf( "offsetof  a: %ld\n", offsetof( struct st, a) );
    printf( "offsetof  b: %ld\n", offsetof( struct st, b) );
    printf( "offsetof  c: %ld\n", offsetof( struct st, c) );
    printf( "offsetof  d: %ld\n", offsetof( struct st, d) );
    printf( "offsetof v2: %ld\n", offsetof( struct st, v2) );
    printf( "offsetof v3: %ld\n", offsetof( struct st, v3) );
    printf( "offsetof v4: %ld\n", offsetof( struct st, v4) );
 
    return 0;
}
 
/*
output:
 
offsetof  a: 0
offsetof  b: 4
offsetof  c: 8
offsetof  d: 16
offsetof v2: 24
offsetof v3: 32
offsetof v4: 44
 
*/

Comments