Skip to content

Commit

Permalink
Add offsetof
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent 11fc259 commit 1b99bad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/stddef.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ typedef long ptrdiff_t;
typedef unsigned int wchar_t;
typedef long max_align_t;

#define offsetof(type, member) ((size_t)&(((type *)0)->member))

#endif
19 changes: 19 additions & 0 deletions test/offsetof.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "test.h"
#include <stddef.h>

typedef struct {
int a;
char b;
int c;
double d;
} T;

int main() {
ASSERT(0, offsetof(T, a));
ASSERT(4, offsetof(T, b));
ASSERT(8, offsetof(T, c));
ASSERT(16, offsetof(T, d));

printf("OK\n");
return 0;
}

0 comments on commit 1b99bad

Please sign in to comment.