{"id":1343,"date":"2023-02-23T11:09:09","date_gmt":"2023-02-23T11:09:09","guid":{"rendered":"https:\/\/ibex.tech\/c\/?p=1343"},"modified":"2023-02-23T11:09:10","modified_gmt":"2023-02-23T11:09:10","slug":"struct","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/memory\/struct","title":{"rendered":"struct"},"content":{"rendered":"\n<p>See also <a href=\"\/c\/c\/memory\/typedef-struct-union\">typedef, struct union<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example struct that will be used multiple times<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Defining a struct called MY_SPECIAL_STRUCT\r\nstruct MY_SPECIAL_STRUCT\r\n{\r\n\tuint8_t MyU8Variable1;\r\n\tuint8_t MyU8Variable2;\r\n\tuint32_t MyU32Variable1;\r\n\tuint32_t MyU32Variable1;\r\n\tuint8_t Spare&#91;20];\r\n};\r\n\r\nstruct MY_SPECIAL_STRUCT MySpecialStruct1;\r\nstruct MY_SPECIAL_STRUCT MySpecialStruct2;\r\n\r\n\/\/Using it:\r\n\/\/\tMySpecialStruct1.MyU8Variable2 = 69;\r\n\r\n\/\/If you need to define them elsewhere as extern:\r\nextern struct MY_SPECIAL_STRUCT MySpecialStruct1;\r\nextern struct MY_SPECIAL_STRUCT MySpecialStruct2;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Example struct for use as one object<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Defining a struct that will be used as MySpecialStruct\r\nstruct MY_SPECIAL_STRUCT\r\n{\r\n\tuint8_t MyU8Variable1;\r\n\tuint8_t MyU8Variable2;\r\n\tuint32_t MyU32Variable1;\r\n\tuint32_t MyU32Variable1;\r\n\tuint8_t Spare&#91;20];\r\n} MySpecialStruct;\r\n\r\n\/\/Using it:\r\n\/\/\tMySpecialStruct.MyU8Variable2 = 69;\r\n\r\n\r\n\/\/If you need to define it elsewhere as extern:\r\nextern struct MY_SPECIAL_STRUCT\r\n{\r\n\tuint8_t MyU8Variable1;\r\n\tuint8_t MyU8Variable2;\r\n\tuint32_t MyU32Variable1;\r\n\tuint32_t MyU32Variable1;\r\n\tuint8_t Spare&#91;20];\r\n} MySpecialStruct;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Checking a struct size<\/h4>\n\n\n\n<p>On 16bit and 32bit platforms your struct may end up being bigger in memory that the sum of its individual variables due to padding bytes. This can be a useful check when you are byte copying it for instance to eeprom memory and back.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define\tEXT_EEPROM_NV_MEMORY_LEN\t\t\t30\n\nstruct NON_VOLATILE_STRUCT\n{\n\tuint8_t MyU8Variable1;\n\tuint8_t MyU8Variable2;\n\tuint32_t MyU32Variable1;\n\tuint32_t MyU32Variable1;\n\tuint8_t Spare&#91;20];\n} NonVolatile;\n\n\n\n\t\/\/----- CHECK FOR NV MEMORY SIZE ISSUE -----\n\tif (sizeof(NonVolatile) > EXT_EEPROM_NV_MEMORY_LEN)\n\t\tprintf(\"WARNING - NonVolatile is larger than EXT_EEPROM_NV_MEMORY_LEN - not all bytes being stored! (NonVolatile is using %i bytes)\\n\", sizeof(NonVolatile));\t\t\/\/This is OK, as long as the end of NonVolatile is unused\n\n\tif (sizeof(NonVolatile) &lt; EXT_EEPROM_NV_MEMORY_LEN)\n\t{\n\t\tprintf(\"CRITICAL ERROR - EXT_EEPROM_NV_MEMORY_LEN NOT BIG ENOUGH! (Change to %i bytes)\\n\", sizeof(NonVolatile));\t\t\/\/This is not OK as ExtEepromRead() will be writing to memory locations after NonVolatile\n\t\twhile (1)\n\t\t\t;\t\t\t\/\/&lt;&lt;&lt;Die here\n\t}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>See also typedef, struct union. Example struct that will be used multiple times Example struct for use as one object Checking a struct size On 16bit and 32bit platforms your struct may end up being bigger in memory that the sum of its individual variables due to padding bytes. This can be a useful check [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-1343","post","type-post","status-publish","format-standard","hentry","category-memory"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/comments?post=1343"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1343\/revisions"}],"predecessor-version":[{"id":1344,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/1343\/revisions\/1344"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=1343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=1343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=1343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}