From 881240d421f35a67276c604689d49f9c16d216f4 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Sun, 14 Apr 2024 22:13:46 +0200 Subject: [PATCH] inline offsetof --- modules/home-assignments/offsetof.md | 15 --------------- modules/structures.md | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-) delete mode 100644 modules/home-assignments/offsetof.md diff --git a/modules/home-assignments/offsetof.md b/modules/home-assignments/offsetof.md deleted file mode 100644 index 5af7d271..00000000 --- a/modules/home-assignments/offsetof.md +++ /dev/null @@ -1,15 +0,0 @@ -# offsetof - -Write a macro (or start with a function with hardcoded values) that will print -the offset of the specified member of a given structure. - -```C - offsetof(struct X, a) -``` - -Hint: exploit the fact that pointer can be assigned an integer (0) + use pointer -arithmetics - -Note: `offsetof()` is a standard macro available since ANSI C via `stddef.h`. - -#solution offsetof.c diff --git a/modules/structures.md b/modules/structures.md index 2e6e4ad7..51819916 100644 --- a/modules/structures.md +++ b/modules/structures.md @@ -203,6 +203,22 @@ de-reference operator on `foo`. #solution struct-access-ptr.c +### :wrench: Getting offset of a member + +Write a macro (or start with a function with hardcoded values) that will print +the offset of the specified member of a given structure. + +```C + offsetof(struct X, a) +``` + +Hint: exploit the fact that pointer can be assigned an integer (0) + use pointer +arithmetics + +Note: `offsetof()` is a standard macro available since ANSI C via `stddef.h`. + +#solution offsetof.c + ## Structure initialization Can initialize a structure in its definition using the initiator list of values.