From 544709fad3b2dd05f22414c1b70be53e6a3504f1 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 18 Feb 2024 18:32:31 +0100 Subject: [PATCH] lib/sizeof.h: memberof(): Add macro This macro is useful to get the size of a member of a structure without having a variable of that type. Reviewed-by: Iker Pedrosa Signed-off-by: Alejandro Colomar --- lib/sizeof.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/sizeof.h b/lib/sizeof.h index f16e1a7bf..6847068ea 100644 --- a/lib/sizeof.h +++ b/lib/sizeof.h @@ -15,10 +15,11 @@ #include "must_be.h" -#define WIDTHOF(x) (sizeof(x) * CHAR_BIT) -#define SIZEOF_ARRAY(a) (sizeof(a) + must_be_array(a)) -#define NITEMS(a) (SIZEOF_ARRAY((a)) / sizeof((a)[0])) -#define STRLEN(s) (NITEMS(s) - 1) +#define memberof(T, member) ((T){}.member) +#define WIDTHOF(x) (sizeof(x) * CHAR_BIT) +#define SIZEOF_ARRAY(a) (sizeof(a) + must_be_array(a)) +#define NITEMS(a) (SIZEOF_ARRAY((a)) / sizeof((a)[0])) +#define STRLEN(s) (NITEMS(s) - 1) #endif // include guard -- 2.47.2