From e3e6d0bb7bd91ea8630813df475a4e1434dad334 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Sun, 13 Dec 2020 23:04:23 +0100 Subject: [PATCH] uint32_t in offset test --- offsetTest/test.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/offsetTest/test.c b/offsetTest/test.c index 4e6ce27..344eac3 100644 --- a/offsetTest/test.c +++ b/offsetTest/test.c @@ -8,18 +8,20 @@ typedef struct { uint8_t id; char a[16]; uint8_t length; + uint32_t x; } t_testObj; -t_testObj testObj = { .id = 0, .a = "test123", .length = 25 }; +t_testObj testObj = { .id = 0, .a = "test123", .length = 25, .x = 123456 }; void printTestObj(t_testObj *testObj) { - printf("testObj: id=%d, a=%s, length=%d\n", testObj->id, testObj->a, testObj->length); + printf("testObj: id=%d, a=%s, length=%d, x=%d\n", testObj->id, testObj->a, testObj->length, testObj->x); } int main() { printf("id: %ld\n", offsetof(t_testObj, id)); printf("a: %ld\n", offsetof(t_testObj, a)); printf("length: %ld\n", offsetof(t_testObj, length)); + printf("x: %ld\n", offsetof(t_testObj, x)); printTestObj(&testObj); @@ -40,6 +42,12 @@ int main() { strcpy((char*)a3, "Wolfgang"); printTestObj(&testObj); + + uint32_t *a4 = (uint32_t*) (((uint8_t*)&testObj) + offsetof(t_testObj, x)); + printf("a4: %p\n", a4); + + *a4 = 1234567890; + printTestObj(&testObj); }