Structure within the union in C

When a variable is associated with a union, the compiler allocates memory by considering the size of the largest memory. Thus, the size of the union is equal to the size of the largest member. therefore, this means that changing the value of any member will change the other values ​​of the elements. but when I execute the following code,

output: 4 5 7.000000
union job
{
    int a;
    struct data
    {
        double b;
        int x
    }q;
} w;


w.q.b=7;
w.a=4;
w.q.x=5;

printf("%d %d %f",w.a,w.q.x,w.q.b);
return 0;
}

The problem is that first I assign the value of a, and then modify the qx value, then the value of a will be overridden by qx But in the output it still shows the original value of a, as well as qx I can not understand why this is happening?

+4
source share
3 answers

- . , , .

, .

, :

aaaa
bbbbbbbbxxxx

, b a. .

double ( Mac 64- Intel. , IEEE754 ):

enter image description here

, Intel "", , , - " ", "".

, , , , , :

#include <stdio.h>
#include <string.h>

void dumpBytes(void *p, int n) {
  int ii;
  char hex[9];
  for(ii = 0; ii < n; ii++) {
    sprintf(hex, "%02x", (char)*((char*)p + ii));
    printf("%s ", hex + strlen(hex)-2);
  }
  printf("\n");
}

int main(void) {
static union job
{
    int a;
    struct data
    {
        double b;
        int x;
    }q;
} w;


printf("intial value:\n");
dumpBytes(&w, sizeof(w));
w.q.b=7;
printf("setting w.q.b = 7:\n");
dumpBytes(&w, sizeof(w));
w.a=4;
printf("setting w.a = 4:\n");
dumpBytes(&w, sizeof(w));
w.q.x=5;
printf("setting w.q.x = 5:\n");
dumpBytes(&w, sizeof(w));

printf("values are now %d %d %.15lf\n",w.a,w.q.x,w.q.b);
w.q.b=7;
printf("setting w.q.b = 7:\n");
dumpBytes(&w, sizeof(w));
printf("values are now %d %d %.15lf\n",w.a,w.q.x,w.q.b);
return 0;
}

:

intial value:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

( static), , ). , 16 , , , double + int, 12 . - 8 , 8- .

setting w.q.b = 7:
00 00 00 00 00 00 1c 40 00 00 00 00 00 00 00 00 

, double, :

40 1c 00 00 00 00 00 00

Sign bit = 0
exponent = 1 0000 0000 0111b  (binary representation)
mantissa = 0

setting w.a = 4:
04 00 00 00 00 00 1c 40 00 00 00 00 00 00 00 00 

a, . , ( ):

00 00 00 00 00 00 04

a 1 ; 0 4 - 15- , .

setting w.q.x = 5:
04 00 00 00 00 00 1c 40 05 00 00 00 00 00 00 00 

5

values are now 4 5 7.000000000000004

. , , b 7 - double .

setting w.q.b = 7:
00 00 00 00 00 00 1c 40 05 00 00 00 00 00 00 00 
values are now 0 5 7.000000000000000

7 double , 00, printf 7.0.

, . - , .

- . .

+5

, : -

#include <stdio.h>

union job
{
    struct data
    {
         int x;
         double b;       
    }q;
    int a;
} w;

int main() {

    w.q.b=7;
    w.a=4;
    w.q.x=5;

    printf("%d %d %f",w.a,w.q.x,w.q.b);
    return 0;
 }

: 5 5 7.000000

, .

+1

w.a = 4 w.q.b. :

After   w.q.b=7;    After   w.a=4;      After   w.q.x=5;

|0|1|0|0|0|0|0|0|   |0|1|0|0|0|0|0|0|   |0|1|0|0|0|0|0|0|   \       \
|0|0|0|1|1|1|0|0|   |0|0|1|1|1|0|0|0|   |0|0|1|1|1|0|0|0|   |   w.a |
|0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |       |
|0|0|0|0|0|0|0|0|   |0|0|0|0|0|1|0|0|   |0|0|0|0|0|1|0|0|   /       |   w.q.b
|0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|           |
|0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|           |
|0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|           |
|0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|           /

-----------------   -----------------   -----------------

|0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   \
|0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |   w.q.x
|0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |
|0|0|0|0|0|0|0|0|   |0|0|0|0|0|0|0|0|   |0|0|0|0|0|1|0|1|   /

, 30- w.q.b 0 1 - 4 4 , , w.q.b .

+1

Source: https://habr.com/ru/post/1524374/


All Articles