From f824e985167f08df80a5cf635809eed6573b2e13 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Thu, 27 Jun 2024 09:02:04 +0100 Subject: [PATCH] doc: Update codeing guidelines for struct initialisation Signed-off-by: Noel Power Reviewed-by: Ralph Boehme Reviewed-by: Douglas Bagnall Reviewed-by: Andreas Schneider Autobuild-User(master): Noel Power Autobuild-Date(master): Fri Jun 28 10:17:14 UTC 2024 on atb-devel-224 --- README.Coding.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/README.Coding.md b/README.Coding.md index 76f2c70e95a..53a829cb4f2 100644 --- a/README.Coding.md +++ b/README.Coding.md @@ -390,6 +390,48 @@ Bad Example: pointer1 = some_func1(); ``` +### Initialize structs + +All structures MUST be at least initialised to 0/NULL. + +Current recommended initialization: + +```c + struct somestruct { + int ival; + bool bval; + double dval; + char *sval; + }; + + struct somestruct var1 = {}; +``` + +avoid: + +```c + struct somestruct var1 = {0}; +``` + +as it can be less portable, in particular if the first element of the struct in question is a nested struct. + +Of course if specific members need non-zero initialization then use something like: + +```c + struct bar { + int inner; + }; + struct foo { + int outer; + struct bar nested; + }; + struct foo var2 = { + .outer = 5, + .nested = { + .inner = 3, + }, + }; +``` ### Make use of helper variables -- 2.47.3