Hi every one..I am new to programming..Can any one tell me the difference between structures and unions in C
There are quite a few difference between struct and union. Structure has memory equal to sum of all member variables where as union has memory equal to hold the largest variable type of all the member variables. In Structure all members variables can be used to store data and retrieve it where as in union only one member variable can be used. You can read more about unions here along with examples.
Hi..To know more details about the difference between structure and union, please refer this link. Structure Union 1.The keyword struct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of sizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, the compiler allocates the memory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset values. 4. The address is same for all the members of a union. This indicates that every member begins at the same offset value. 5 Altering the value of a member will not affect other members of the structure. 5. Altering the value of any of the member will alter other member values. 6. Individual member can be accessed at a time 6. Only one member can be accessed at a time. 7. Several members of a structure can initialize at once. 7. Only the first member of a union can be initialized.
Structure has memory equal to sum of all member variables where as union has memory equal to hold the largest variable type of all the member variables.
Structure: A structure is a user-defined data type available in C that allows to combining data items of different kinds. Structures are used to represent a record. Defining a structure: To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows: Code: struct [structure name] { member definition; member definition; ... member definition; }; Union: A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes. Defining a Union: To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows: Code: union [union name] { member definition; member definition; ... member definition; }; Thanks!
Structure and Union both are user defined data types but the major difference between both of them is that in structure each member gets separate space in memory whereas in Union the memory allocated is equal to the member with largest size.