Home > Back-end >  Small white inquired about by the union share structure hybrid array
Small white inquired about by the union share structure hybrid array

Time:11-09

Today read a book to see array can be mixed with the union structure data types such as
Typedef union
{
int a;
Char b;
} A
A array [100]
Then according to the book: "each array element array, there is A type of array two members, can store data type int, can also be stored char data type"
But have a few questions: 1. 100 elements in the array is int and char type half-and-half every 50 or can customize?
2. The "two members" what meaning be? Refers to a half or as long as I have an int in the array and char can can have an int and 99 char?
Or only one int a char?
3. I found in my tests into the string in the array can also print (prove he) in the array but I defined in the union is not defined in the char array type means should not save string, but, but it printed out!
4. The test when I add a float is c: typedef union
{
int a;
Char b;
Float c;
} A
A array [101]
Corresponding saved a decimal, but print out is 0.000000, float az why??
Small white inquire! Thanks to answer!

CodePudding user response:

How do you write???????

CodePudding user response:

Is the union members to share a piece of memory space
For example,
The union {
int a;
Char b;
}
Have a, b two members, but because space is Shared, int the largest space (4 bytes), so will be subject to it, a member USES 4 bytes, b members using a byte, exists in a b, the following figure,
The space of whole union for @ @ @ @
A @ @ @ @
B @
Is the first byte of a and b memory space overlap
If the assignment b=1, memory into
A 1 @ @ @
B 1
If the assignment a=1, memory into a
A 0001
B 0
Visible change could affect the b (because Shared memory), the same change b may also affect a
Compare the struct
Struct {
int a;
Char b;
}
A and b is not Shared space, so the whole structure of the space is 4 bytes int + 1 byte=5 bytes of char (regardless of the first memory alignment, if consider alignment is 8 bytes), memory distribution below
The space of whole struct for @ @ @ @ @
A @ @ @ @
B?????? @
? Said not to use the space, also is the memory space of a and b have no overlap, after a 4 bytes before use, b use 1 byte
If the assignment b=1, memory into
A @ @ @ @
B?????? 1
If the assignment a=1, memory into a
A 0001
B?????? 1
Visible change does not affect the b (because memory independent of a and b), the same change b nor a

So the union is for members to share a piece of memory space, a member of the change, other members could change

So
Question 1100 elements are the union types, each element can be put int or char, users choose
For example,
Array [0]. A=1;//save int
Array [1]. B='a';//save char

Question 2
Two members of a and b, a and b share the same block of memory

Question3:
Data storage is the smallest unit bytes (char), any multibyte type (actually) is equivalent to a char array can be a string, but with different values of memory storage,
For example,
int a;
Strcpy ((char *) & amp; A, "ab");//int save string
Printf (" % s ", & amp; A);//print string
So you an array of strings is fine, as long as the better memory control cross-border can

Question 4
How you assignment, the above said, members are Shared memory space, give a member assignment may affect other members, estimation is your assignment is not right,



  • Related