Related questions

0 votes
0 answers 1 view
0 votes
1 answer 25 views
0 votes
1 answer 7 views
0 votes
1 answer 2 views
2 views asked Jun 5 in Python by anonymous
0 votes
0 answers 4 views
Welcome to Python- Question2Answer, where you can ask questions and receive answers from other members of the community.

What is the difference between structs and unions?

0 votes

What is the difference between structs and unions?

asked Jul 13 in C program by admin (460 points)

1 Answer

0 votes

A struct is a complex data type that allows multiple variables to be stored in a group at a named block of memory. Each member variable of a struct can store different data, and they all can be used at once.

struct a {
    int x;
    char y;
} a;

For example, you may store an integer in x, and and a character in y above, independent of each other.

A union, on the other hand, stores the contents of any member variable at the exact same memory location. This allows the storage of different types of data at the same memory location. The result is that assigning a value to one member will change the value of all the other members. Unlike struct, only one member of the union type is likely to be useful at any given time.

union b {
    int x;
    char y;
} b;

For example, storing a character in y may automatically change the integer you read from x to something meaningless or unpredictable.

answered Jul 13 by admin (460 points)
...