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
4 answers

Regarding printing array

I'm new in C. Let's say i have a union. union DP { int c[3]; char a[3][4]; short b[2]; }point = {256,258,260}; printf("%i",point.a[0][1]); printf("%i",point.a[1][2]); printf("%i",point.a[2][0]); Why does the first print give 1 and…
0
votes
2 answers

Values in union in C

#include int main() { union a { int i; char ch[2]; }; union a u; u.ch[0] = 0; u.ch[1] = 2; u.ch[2] = 0; u.ch[3] = 0; printf("%d\n",u.i); return 0; } In this program, if the size…
user3845968
  • 113
  • 7
0
votes
7 answers

unions declaration C code

I have seen some declaration of a union inside a struct as follows. Example code given below. My questions is does it help in any memory savings(typical use for which a union is used for)? I do not see the benefit. typedef struct { int x1; …
goldenmean
  • 18,376
  • 54
  • 154
  • 211
0
votes
3 answers

Is using the most significant bit to tag a union considered a bad practice?

Suppose I have the following tagged union: // f32 is a float of 32 bits // uint32 is an unsigned int of 32 bits struct f32_or_uint32 { char tag; union { f32 f; uint32 u; } } If tag == 0, then it is a f32. If tag == 1,…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
0
votes
1 answer

SQLite: Trying to concatenate columns in more elegant way than string of UNIONs

I have a table with the following fields and example value: coursecode BIO 101 a_code FA b_code SP c_code SU d_code e_code I'm trying to populate a field in another table (using coursecode as the key) with a comma separated list of…
0
votes
1 answer

Why is the output 0 2 all the time when it should be garbage?

#include int main() { union a { int i; char ch[2]; }; union a z = {512}; printf("%d %d",z.ch[0],z.ch[1]); return 0; } The output is: 0 2 Why is the output 0 2, when it should be some garbage value?
Sagar
  • 273
  • 1
  • 3
  • 12
0
votes
2 answers

union memory allocation :: stores more content than allocated memory

I have defined a union as union person { int roll; char fname[10]; char lname[20]; }p1; And the sizeof(p1)=20 bytes. but while storing the content in p1 its storing more than 20 chars. void main() { printf("\n The size of the…
alpha9eek
  • 1,439
  • 3
  • 11
  • 10
0
votes
3 answers

Using UNIONs and STRUCTUREs

Is this approach correct? struct netinfo { // a lot of members here... union { UINT type; struct { UINT _type; // a place holder (obviously who access this struct already knows the type is ipv4) UINT32…
user3525723
  • 313
  • 1
  • 8
0
votes
1 answer

How to use struct within an union, within a struct in C?

i am currently having a lot of struggle with a, for me personally, very complex structure struct crypto_tfm { uint32_t crt_flags; union { struct ablkcipher_tfm ablkcipher; struct aead_tfm aead; struct…
user3694354
  • 105
  • 8
0
votes
0 answers

COM interop - marshaling unions of value and reference types by overloading interface methods

Consider the following method from PInvoke.dll: union MYUNION2 { int i; char str[128]; }; PINVOKELIB_API void TestUnion2( MYUNION2 u, int type ) { //...implementation is irrelevant... } According to MSDN: In managed code, value types…
Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
0
votes
2 answers

Structure is filled incorrectly during fread_s

I have the following structure defined ( u8 is typedef for unsigned char) struct { u8 length_directory_record; u8 extended_attribute_record; u8 location_of_extend[8]; union { u8 bytes[8]; struct { …
0
votes
1 answer

What is wrong with this structure/union declaration?

I declared this structure in global scope, struct a{ int a; int x; union b{ int a; int b; int y; }; }; And then declared an union inside main, union b a; And this is not giving any errors. But if declare union 'a' in the definition of structure,…
nishantbhardwaj2002
  • 757
  • 2
  • 6
  • 18
0
votes
1 answer

How do add this long variable without corrupting the following union?

Hi guys I want to add this long variable l_cliend_id without corrupting the data in this union union _V2_INPUT { struct _V2_HEADER header; struct _IN_DETAIL detail; struct _V2_TRAILER trailer; }; How…
user3419585
  • 13
  • 1
  • 1
  • 7
0
votes
3 answers

Struct inside union inside struct in c

hello lets say i have this code typedef struct entry { union { struct A { char *c; } *A; struct B { char *c; } *B; } value; } *TableEntry; i m doing a malloc for entry and now i want…
0
votes
1 answer

Compilation error "has no member named" for a union member

I see this compilation error "has no member named" for some of the union elements. snmp_xmas.c:129: error: 'xmas_datatype_value_t' has no member named 'int8' snmp_xmas.c:132: error: 'xmas_datatype_value_t' has no member named 'int16' I dont see…
Abhi V
  • 89
  • 1
  • 3
  • 10