unions

type namespace in C

风格不统一 提交于 2020-01-11 09:36:13
问题 I've read in SO about different namespaces in C where the type are defined, e.g. there is a namespace for Structs and Unions and a namespace for typedefs. Is namespace the exact name for this? How many namespaces exist in C? 回答1: see 6.2.3 from http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf 6.2.3 Name spaces of identifiers If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to

Union tested for current member in use

时光怂恿深爱的人放手 提交于 2020-01-11 02:07:29
问题 Do unions have a control structure to test which member is currently in use (or if it has any at all)? I'm asking this because undefined behavior is never a good thing to have in your program. 回答1: No, no such mechanism exists off-the-shelf. You'll have to take care of that yourself. The usual approach is wrapping the union in a struct : struct MyUnion { int whichMember; union { //whatever } actualUnion; }; So you have MyUnion x; and x.whichMember tells you which field of x.actualUnion is in

What is this syntax: union{}?

∥☆過路亽.° 提交于 2020-01-06 16:14:29
问题 void display_binary_float(unsigned int ui) { union { unsigned int ui; float f; } uif2; uif2.ui = ui; printf("binary: %08X float: %g\n", uif2.ui, uif2.f); } 1) What is union? There was no manual entry for it, for some reason. Couldn't find the doc on google. 2) Why is uif2 at the end of the function union? Shouldn't it be something like union uif2 {} or is this a C thing? 回答1: Wikipedia, it say: In C and C++, untagged unions are expressed nearly exactly like structures (structs), except that

Structure and tagged union in c

独自空忆成欢 提交于 2020-01-06 15:32:09
问题 #define HOST_NAME "UDP" #define ADDRESS "127.0.0.1" struct UDP_IP_Parameters { uint version; /* e.g. "1.0" = 0x0100 */ uint port; /* PORT */ taggedunion { "HOST_NAME" char[256]; "ADDRESS" char[15]; }; }; int main() { struct UDP_IP_Parameters udp; udp.version = 0x0100; udp.port = 444; } I have created a structure and taggedunion nested within that. Is it possible to define the host name and address as constant like above ?? Is it possible to assign some values by creating a objects for it.

Assigning individual bits to bytes

懵懂的女人 提交于 2020-01-06 04:51:07
问题 I have a to make a SPI communication between a microcontroller and another chip. The chip accepts a 16bit word. But the abstraction library requires the data to be sent as two 8bit bytes. Now I want to make a wrapper so I can easily create requests for read and write...but I have not yet got any success. Here is how it supposed to be: The table below shows 16bits. The MSB can be 0 for write or 1 for read. The address can be from 0x0 to 0x7 and the data is 11 bits. R/W | ADDRESS | DATA B15 |

What is the correct way to check equality between instances of a union?

梦想与她 提交于 2020-01-03 11:02:38
问题 I have a multithreaded application that stores data as an array of instances of the following union union unMember { float fData; unsigned int uiData; }; The object that stores this array knows what type the data in the union is and so I dont have problems with UB when retrieving the correct type. However in other parts of the program, I need to test equality between 2 instances of these unions and in this part of the code the true internal data type is not known. The result of this is that I

C: typedef union

独自空忆成欢 提交于 2020-01-03 10:42:48
问题 didn't find anything in related questions. Most probably it's super noob, but I'll ask anyway/ I've got the following in my .h file: typedef union _API_Packet_0x90{ uint8_t packet[26]; struct _pack_struct { uint8_t start; uint8_t length[2]; uint8_t addr64[8]; uint8_t addr16[2]; uint8_t options; uint8_t rfData[4]; uint8_t chksum; }; } API_Packet_0x90; API_Packet_0x90 ap90; This is code for a microcontroller, I'm using xc8 toolchain (former Hi Tech C). The compiler says: xbee_api.h:19: warning:

Error: illegal cast: from 'int' to 'union'

只愿长相守 提交于 2020-01-03 01:34:10
问题 I'm getting the error, illegal cast: from 'int' to 'FIELDS' while initializing the structure variables here:- SOCKET_LOG_DATA socket_log_data() : fields(0), socket_number(0) {} How should I resolve it? typedef PACKED struct PACKED_SUFFIX { UINT16 loss_reason : 1; UINT16 unused : 15; } LOSS_REASON; typedef union PACKED_SUFFIX { LOSS_REASON loss; UINT16 all_fields; } FIELDS; typedef PACKED struct PACKED_SUFFIX SOCKET_LOG_DATA { FIELDS fields; UINT16 socket_number; // As per @Dietrich's &

Will this bitfield work the way I expect?

让人想犯罪 __ 提交于 2020-01-02 06:49:06
问题 I've been doing some reading about bitfields in C, how the C standard doesn't enforce any particular ordering of the fields in a machine word, and so on. I hope this question appropriately fits the format of SO. My question is whether my struct (definition following) will actually perform in the way I expect. Here's the definition I came up with, and then I'll discuss what I want: typedef enum { STATE_ONE, STATE_TWO, STATE_THREE, STATE_FOUR } __attribute__ ((packed)) State; typedef struct

initialize a union array at declaration

女生的网名这么多〃 提交于 2020-01-02 03:45:08
问题 I'm trying to initialize the following union array at declaration: typedef union { __m128d m; float f[4]; } mat; mat m[2] = { {{30467.14153,5910.1427,15846.23837,7271.22705}, {30467.14153,5910.1427,15846.23837,7271.22705}}}; But I'getting the following error: matrix.c: In function ‘main’: matrix.c:21: error: incompatible types in initialization matrix.c:21: warning: excess elements in union initializer matrix.c:21: warning: (near initialization for ‘m[0]’) matrix.c:21: warning: excess