What is the difference between structure and union | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What is the difference between structure and union

4th Dec 2018, 11:42 AM
Jagadish Kumar
Jagadish Kumar - avatar
5 Answers
+ 12
Code playground has the answer😎👍
11th Apr 2019, 5:26 AM
OkomoJacob
OkomoJacob - avatar
+ 6
What's a struct? "A struct in the C programming language (and many derivatives) is a composite data type (or record) declaration that defines a physically grouped list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer, or the struct declared name which returns the same address. The struct data type can contain many other complex and simple data types in an association, so it is a natural type for organizing mixed-data-type records such as lists of hard-drive directory entries (file length, name, extension, physical (cylinder, disk, head indexes) address, etc.), or other mixed-type records (patient names, address, telephone... insurance codes, balance, etc.). [...] ¹" What's a union? "In computer science, a union is a value that may have any of several representations or formats within the same position in memory; or it is a data structure that consists of a variable that may hold such a value. Some programming languages support special data types, called union types, to describe such values and variables. In other words, a union type definition will specify which of a number of permitted primitive types may be stored in its instances, e.g., "float or long integer". Contrast with a record (or structure), which could be defined to contain a float and an integer; in a union, there is only one value at any given time. [...] ²" ______ ¹ https://en.wikipedia.org/wiki/Struct_(C_programming_language) ² https://en.wikipedia.org/wiki/Union_type
4th Dec 2018, 12:24 PM
Babak
Babak - avatar
+ 1
Sizeof structure is the size of its individual members but whereas in Union.., size of union is the highest data type present in it. Structure has different memory locations for each of its member but Union shares the memory location with each member where previous value is replaced.
23rd Sep 2019, 9:33 AM
A.Sudheer Rajkumar
+ 1
Structure is a user defined data type. The size of the structure is the sum of all the data types which are declared inside the block like float char or int..... It can access all the datatypes at a time. Example:- Student details. Like his name is stored in char and his marks are stored in int and his percentage is stored in float. I.e.., the sum of all the float, char and int is occupied in structure. But, where as in union. It holds only the highest size of all the data types in the block. It only access one data type at a time. So no need to store the all sizes.
29th Sep 2019, 11:46 AM
Sankar Rao Mudadla
Sankar Rao Mudadla - avatar
0
Structure is used to store different types of data. We write different type of members in struct. For each member of structure separate memory is allocated at different location . Union:- It used to store different type of data . We write members inside union. For each member common memory is allocated . Size of memory allocated to the union is equal to the largest size data type declare in union . Ex . union demo{ int a; double b; }; So memory allocated is 8 bytes (depends on architecture). So note here that common memory is allocated for all member. They use it as they need .
21st Apr 2019, 1:45 PM
Chetan Patil