answersLogoWhite

0


Best Answer

Struct and Union have following similarities:- Both are aggregated data types that allow a mixed for values to be contained in their instances.They are the way aggregation is done in C for making. The difference between them is that union allows the different items to be in it but allows access to be on one becase they are represented in overlapped manner. A Struct is also aggregation but it separates each data item contained in it and therefore anyone can be accessed anytime from its instance variable.Unions are ordinarily used because of performance and generalization ,while the structures are generally used for aggregation. For queries Rupesh Kumar Joshi ,rupesh_joshi@sify.com

User Avatar

Wiki User

18y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Similarities between struct and union
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between struct and union in c file management?

The main difference is in how the data structures are stored. In a union, all of the elements are stored in one location. A structure stores each of its elements in a separate memory location.


Similarities between C plus plus struct and class?

The only difference between a struct and a class in C++ is that struct members are public by default while class members are private by default. Other than that they act and behave in exactly the same way and are both used to define classes. By convention, struct is used exactly as it would be used in C, with all public members and no member functions, to provide backward compatibility with C-style code.


Similarities between Uk and Australia?

They are both part of the national union.


What is a possible connection between instructions and the root struct?

What_is_a_possible_connection_between_instructions_and_the_root_struct


Are there any similarities between Russian federation and soviet union?

Yes. They still basically have the same type of Government


Is a struct or a union more preferable in C programming?

They both do entirely different things so the preferred option is the one that fulfils your needs. A struct is used to combine two or more data types (members) into a single entity, where each member is stored and accessed separately within a contiguous block of memory. The size of a struct is equal to the total size of all its members, plus any padding for alignment purposes. A struct is not unlike a database record with two or more fields. However, to conserve memory and minimise padding, members must be declared in order of size, from largest to smallest. A union is used to store a single data type that can be re-interpreted, not unlike a static cast. The largest member determines the overall size of the union and the individual members determine how the memory is to be interpreted. Since all members effectively refer to the same memory address, only one member of a union may be accessed at any one time. Changing the value of any one member affects all members. Structs and unions can be combined both ways, so a struct may contain a union (allowing the same struct member to be re-interpreted) and a union may contain a struct. The latter form makes it possible for a union to contain a primitive data type, such as a 32-bit integer, as well as a struct member that contains two or more members that have a combined size of 32-bits, such as four separate 8-bit members (bytes). Changing the value of any one byte in the struct affects a different byte within the 32-bit integer because both the integer and the struct are mapped to the same address space.


What is the difference between declaring variable and initializing variables?

Actually, there is a third step, call definition. Declaration is a statement to the compiler of what type an identifier is, definition is the allocation of memory for that identifier, and initialization is the assignment of an initial value to that identifier. Usually, declaration and definition are done together, but you can also add initialization in that step if desired. int a; /* declaration and definition */ a = 1; /* initialization */ int a = 1; /* declaration, definition, and initialization */ For the case of seperate declaration and definition, consider the struct... struct _mystruct { int a; }; /*declaration */ struct _mystruct mystruct; /* definition */ struct _mystruct { int a; } mystruct; /*declaration and definition */ Note: To be more precise: struct _mystruct; /* struct declaration */ struct _mystruct { int a; }; /* struct definition */ typedef struct _mystruct MYTYPE; /* type definition */ extern struct _mystruct mystructvar; /* variable declaration */ struct _mystruct mystructvar; /* variable definition */ struct _mystruct mystructvar = {7} ; /* variable definition with initialization */ struct _mystruct { int a; } mystruct; /* struct definition and variable definition */ extern struct _mystruct { int a; } mystruct; /* struct definition and variable declaration */


How does the root struct in destruction help you understand?

What does struct mean in in


What is the meaning of the word struct?

"struct" is not a word.


What are the similarities between basketball and ring-ballwhat are the similarities between basketball and ring-ballwhat are the similarities between basketball and ring-ball?

what are the similarities between basketball and ring-ball


Is there a limit to nesting of structure?

Yes: only completely defined structured can be included (that doesn't include the actual structure itself). Example: struct a; struct b { int b1; }; struct c { struct a; /* BAD */ struct b; /* OK */ struct c; /* BAD */ };


Similarities between the ulna and the radius?

what is the similarities between the ulna and the radius