{"id":30,"date":"2010-06-09T08:54:55","date_gmt":"2010-06-09T08:54:55","guid":{"rendered":"http:\/\/www.electronic-products-development.com\/?p=30"},"modified":"2022-02-17T10:13:11","modified_gmt":"2022-02-17T10:13:11","slug":"typedef-struct-union","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/memory\/typedef-struct-union","title":{"rendered":"Typedef, struct union"},"content":{"rendered":"<h4>Typedef Union For Different Definitions Of The Same Memory Locations<\/h4>\n<pre><code>\ntypedef union _SYSTEM_CONFIGURATION_TYPEDEF\n{\n    DWORD dw[25];\n    WORD w[50];\n    BYTE b[100];\n} SYSTEM_CONFIGURATION_TYPEDEF;\n\nSYSTEM_CONFIGURATION_TYPEDEF system_config_buffer;\n\n\tsystem_config_buffer.b[0] = 1;\n\tsystem_config_buffer.b[1] = 2;\n\tsystem_config_buffer.b[2] = 3;\n\tsystem_config_buffer.b[3] = 4;\n\tsystem_config_buffer.b[4] = 5;\n\t\/\/Will give:\n\t\/\/system_config_buffer.w = 0x0201\n\t\/\/system_config_buffer.dw = 0x04030201\n<\/code><\/pre>\n<h4>Typedef Struct<\/h4>\n<pre><code>\ntypedef struct _TEST_HEADER\n{\n  WORD source_port;\n  DWORD sequence_number;\n  WORD window;\n} TEST_HEADER;\n<\/code><\/pre>\n<h4>Typedef Struct For A Buffer Of Different Registers In An Buffer<\/h4>\n<pre><code>\ntypedef struct _TEST_HEADER\n{\n\tWORD source_port;\n\tDWORD sequence_number;\n\n\tunion\t\t\/\/&lt;&lt;&lt; You can also have unions inside a struct\n\t{\n\t\tstruct\n\t\t{\n\t\t\tBYTE flag_fin\t:1;\n\t\t\tBYTE flag_syn\t:1;\n\t\t\tBYTE flag_rst\t:1;\n\t\t\tBYTE flag_psh\t:1;\n\t\t\tBYTE flag_ack\t:1;\n\t\t\tBYTE flag_urg\t:1;\n\t\t\tBYTE reserved\t:2;\n\t\t} bits;\n\t\tBYTE byte;\n\t} flags;\n\n\tWORD window;\n} TEST_HEADER;\n<\/code><\/pre>\n<h4>A Union To Convert Float To Bytes And Bytes To Float<\/h4>\n<pre><code>\nunion FLOAT_BYTES\n{\n\tfloat f;\n\tBYTE v[sizeof(float)];\n};\n\/\/HOW TO USE:\n\/\/\tFLOAT_BYTES my_value;\n\/\/\tmy_value.f = 0.1234;\n\/\/\tBYTE x = my_value.v[0];\n<\/code><\/pre>\n<h4>Handy Conversions<\/h4>\n<pre><code>\n\ntypedef union _MODBUS_ARRAY_TYPEDEF\n{\n    WORD w[(NV_MEMORY_LENGTH &gt;&gt; 1)];\n    BYTE v[NV_MEMORY_LENGTH];\n} MODBUS_ARRAY_TYPEDEF;\n\ntypedef union _FLOAT_BYTES\n{\n\tfloat f;\n\tuint16_t w[2];\n\tuint8_t v[4];\n}FLOAT_BYTES;\n\/\/HOW TO USE:\n\/\/\tFLOAT_BYTES my_value;\n\/\/\tmy_value.f = 0.1234;\n\/\/\tBYTE x = my_value.v[0];\n\ntypedef union _DOUBLE_BYTES\n{\n\tdouble d;\n\tWORD w[2];\n\tBYTE v[4];\n}DOUBLE_BYTES;\n\ntypedef union _SIGNED_WORD_BYTES\n{\n\tSIGNED_WORD sw;\n\tWORD w;\n\tBYTE v[2];\n}SIGNED_WORD_BYTES;\n\n<\/code><\/pre>\n<h4>Union and Struct combined<\/h4>\n<pre><code>\n#define\tNV_MEMORY_LENGTH\t\t\t20\ntypedef struct _NON_VOLATILE_STRUCT\n{\n\tunion\n\t{\n\t\tstruct\n\t\t{\n\t\t\tuint32_t my_u32_val_1;\n\t\t\tuint32_t my_u32_val_2;\n\t\t\tuint16_t my_u16_val_1;\n\t\t\t\/\/&lt;&lt;&lt;Add others here\n\t\t} val;\n\t\tuint8_t b[NV_MEMORY_LENGTH];\n\t};\n} NON_VOLATILE_STRUCT;\n\/\/non_volatile.val.\n\/\/non_volatile.b[]\n\nNON_VOLATILE_STRUCT non_volatile;\n\n\n\tnon_volatile.val.my_u32_val_1 = 0x12345678;\n\tnon_volatile.b[0] = 0x12;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Typedef Union For Different Definitions Of The Same Memory Locations typedef union _SYSTEM_CONFIGURATION_TYPEDEF { DWORD dw[25]; WORD w[50]; BYTE b[100]; } SYSTEM_CONFIGURATION_TYPEDEF; SYSTEM_CONFIGURATION_TYPEDEF system_config_buffer; system_config_buffer.b[0] = 1; system_config_buffer.b[1] = 2; system_config_buffer.b[2] = 3; system_config_buffer.b[3] = 4; system_config_buffer.b[4] = 5; \/\/Will give: \/\/system_config_buffer.w = 0x0201 \/\/system_config_buffer.dw = 0x04030201 Typedef Struct typedef struct _TEST_HEADER { WORD source_port; [&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,12],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-memory","category-typedef"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/30","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=30"}],"version-history":[{"count":9,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":1221,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/30\/revisions\/1221"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}