Questions tagged [unions]

The plural of a keyword in the C family of languages for declaring a union data type.

A union is a single variable with multiple possible representations. For example:

union foo {
    int a;
    float b;
    struct {
        char c, d;
    } e;
};

is a variable foo that can be accessed as either an int, float, or struct. Although similar in format to a struct, the size of a union is equal to the size of the largest field where as the size of a structure is the sum of the sizes of all fields.

They can also be used for a primitive form of polymorphism. A program can check for types and use different fields in a union in different contexts.


For the SQL UNION keyword, use instead of this tag.

1638 questions
0
votes
1 answer

Anonymous union/structure holding a generic vector

I'm trying to create an anonymous union that holds a generic vector in order to use it as a member in a class without naming the type_name of the union itself. So that I could call the vector inside the class as following: vec.size(); But my…
0
votes
5 answers

incrementing union data in c

I am somewhat unfamiliar with unions, have done some reading on them and am having trouble figuring this out. Someone has defined a union for me: union CANDATA // Unionize for easier cooperation amongst types { unsigned long long ull; …
Troy Cosentino
  • 4,658
  • 9
  • 37
  • 59
0
votes
2 answers

Bit fields in a union - how portable is this?

I got a bit field with a bunch of flags, and I need a quick and dirty way to set everything to zero, so instead of blindly casting the struct to an integer, I decided it would be "better" to put the bit fields in a union with an actual integer.…
user2341104
0
votes
1 answer

One array in another

I have a union: typedef union element{ int number; char letter; } Element; // used typedef for faster writing of code I then went ahead and created an array of unions, limited to 10 unions. Therefore, I have: Element set[10]; As such, I…
user3182162
0
votes
1 answer

"invalid type argument of unary '*' C

I'm relatively new to to programming and even after doing a thorough research I am not able to solve this problem. I want to check whether through an object [defined as of variable type union] I can compare with a new object entered by the user for…
0
votes
1 answer

Marshall Struct containing unions with arrays

I'm trying to marshall the following unmanaged c struct in c# typedef struct { PNIO_ADDR_TYPE AddrType; /* Enum: size:32bits */ PNIO_IO_TYPE IODataType; /* Enum: size:32bits */ union { PNIO_UINT32 Addr; /* logical address */ PNIO_UINT32 …
jero2rome
  • 1,548
  • 1
  • 21
  • 39
0
votes
2 answers

How can I allocate 10bit values to a byte array?

I need to send data from a microcontroller through a serial port and would like to minimize the data sent, because the data is a 10bit reading from an ADC. So I thought, I would use an union. This is what I got: union fortybit { struct { …
Matouš Vrba
  • 175
  • 1
  • 8
0
votes
1 answer

Structure and tagged union in c

#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]; …
user2984410
  • 39
  • 2
  • 7
0
votes
2 answers

Access union members of array of unions

typedef union { unsigned i; float x; } f; f array[12]; What do I need to do to address the union members in an array like this? If not possible, how can I do this?
0
votes
1 answer

using union in yacc for structures

I'm a little confused about how to specify my grammar member's type. I want to declare prog and decls as ASTNode. I'm gonna use these members for adding to a list or etc. But yacc can't recognize them as an ASTNode and I get type errors. Here my…
iva123
  • 3,395
  • 10
  • 47
  • 68
0
votes
1 answer

C : pointer and union and address

I want to print out some address on a value. The following is what I got and got to print out what I want. // I need the first address to start with zero. void printoutAddr(char* x, int n, int sign) { printf(" Address +x00 …
user2671513
0
votes
1 answer

C : assign value to uninitialzed struct and union

I am given two global variables, in pointer. All I need to do is assign character value to some local variables checking boundaries. // global variable char *min_ptr, *max_ptr ; void markchar( char *x, int size, char marked) { // for every…
user2671513
0
votes
1 answer

typedef union in C and eclipse autocomplete

I'm trying to declare a typedef union for a time format as follows, In the header I have : typedef union _u_time { unsigned long l_time; struct { unsigned char :8; unsigned char HRS; unsigned char MIN; …
Toon
  • 3
  • 5
0
votes
2 answers

extra data elements in a union structure

I'm trying to encode a relatively complex message into a union structure so that I can generate an array of uint8_t that I can feed into a serial communications protocol. However, when looking at the array generated when my union is filled with…
rocklegend
  • 81
  • 11
0
votes
4 answers

Union usage and messy code

I have some code which is about the usage of union shown as following: int main(){ typedef union{int a;char b[10];float c;}Union; Union x,y = {100}; printf("Union x :%d| |%s| |%f \n",x.a,x.b,x.c ); printf("Union y :%d| |%s| |%f…
Alex Lee
  • 161
  • 2
  • 8
1 2 3
99
100