What's the major difference between “union” an

2019-03-14 12:12发布

Possible Duplicate:
Difference between a Structure and a Union in C

I could understand what a struct means. But, i am bit confused with the difference between union and struct. Union is like a share of memory. What exactly it means.?

7条回答
2楼-- · 2019-03-14 12:30

It may be more useful to have an uncontrived example of what this is good for. (I say "uncontrived" because most bit-banging uses of union are extremely treacherous. Bit-banging unions taken from big-endian to little-endian hardware break in the most (initially) mystifying ways.) (Of course, I've written bit-banging unions to tear apart floating point numbers to implement orders-of-magnitude-faster-than-the-library math functions. I just add assertions about which members are supposed to have the same addresses.)

    struct option1 { int type; /* other members */ };
    struct option2 { int type; /* other members */ };
    struct option3 { int type; /* other members */ };
    union combo {
      int type; // guaranteed to exactly overlap with the structs' ints type.
      struct option1;
      struct option2;
      struct option3;
    };
   // ...
   void foo(union combo *in) {
      switch(in.type) {
        case 1: { struct option1 *bar = in;  //then process an option1 type of request }
        case 2: { struct option2 *bar = in;  //then process an option2 type of request }
        case 3: { struct option3 *bar = in;  //then process an option3 type of request }
      }

This kind of construction is very common in X programming and other situations where one wishes to make a function that can receive many different types of messages (with different argument and layout requirements).

查看更多
Juvenile、少年°
3楼-- · 2019-03-14 12:34

I've used unions to convert bytes to and from other types. I find it easier than bit-shifting.

union intConverter {
    int intValue;
    struct {
        byte hi;
        byte lo;
    } byteValue;
}

intConverter cv;
cv.intValue =1100;
printf("%X %X\n", cv.byteValue.hi, cv.byteValue.lo);

Where int is 16-bit (was used on a micro controller).

查看更多
一纸荒年 Trace。
4楼-- · 2019-03-14 12:36

Each member of a union shares the same memory. That means if you change one, you change the others. And if the members are of different types, this can have unpredictable results. (not exactly unpredictable, but hard to predict unless you are aware of the underlying bit patterns that make up the data members).

查看更多
Deceive 欺骗
5楼-- · 2019-03-14 12:36

Most answers here are correct. A union is essentially a way to access same data in different ways (For example, you can access/interpret 4 bytes of memory as 1 integers, or as 4 characters). Structs as you know are straightforward - a collection of different, seprate objects with their own memory.

Usually you require Unions at a much later stage in programming as compared to Structs.

查看更多
萌系小妹纸
6楼-- · 2019-03-14 12:40

I suppose one way you can think of a union is that it is a set of aliases of varying type to a block of memory where each member of the union is an "alias" with a given type. Each alias refers to the same address in memory. How the bits at that address are interpreted are determined by the alias' type.

The amount of memory the union occupies is always equal to or possibly larger than the largest sized "member" of the union (due to alignment restrictions).

查看更多
倾城 Initia
7楼-- · 2019-03-14 12:40

Run this program and find out the output.

#include < stdio.h >

int main()
{
  union _testUnion
  {
    long long x;
    long long y;
  } testUnion;

  struct _testStruct
  {
    long long x;
    long long y;
  }testStruct;
  printf("Sizeof Union %d\n",sizeof(testUnion));
  printf("Sizeof Struct %d\n",sizeof(testStruct));
  return;
}

You will find that the size of struct is double than that of union. This is because union has allocated space for only one variable while struct has allocated for two.

查看更多
登录 后发表回答