C

Topic: Structures and union

Describe structures and Union in brief.

Structures:
Structures provide a convenient method for handling related group of data of various data types. 

Structure definition: struct tag_name { data type member1; data type member2; … …}

Example: 
struct library_books { 
char title[20]; 
char author[15]; 
int pages; float price; 
};

The keyword struct informs the compiler for holding fields ( title, author, pages and price in the above example). All these are the members of the structure. Each member can be of same or different data type.

The tag name followed by the keyword struct defines the data type of struct. The tag name library_books in the above example is not the variable but can be visualized as template for the structure. The tag name is used to declare struct variables.

Ex: struct library_books book1,book2,book3; The memory space is not occupied soon after declaring the structure, being it a template. Memory is allocated only at the time of declaring struct variables, like book1 in the above example. The members of the structure are referred as - book1.title, book1.author, book1.pages, book1.price.UnionsUnions are like structures, in which the individual data types may differ from each other. All the members of the union share the same memory / storage area in the memory. Every member has unique storage area in structures. In this context, unions are utilized to observe the memory space. When all the values need not assign at a time to all members, unions are efficient. Unions are declared by using the keyword union , just like structures.

Ex: union item { int code; float price;};

The members of the unions are referred as - book1.title, book1.author, book1.pages, book1.price.

Browse random answers:

What is a priority queue?
How to build an expression trees ?
How to convert Stack in to Queue and v varsa c with data structure.?
How do display the list in single linked list form last to first?
What is the easiest sorting method to use? 
What is the quickest sorting method to use? 
What is the quickest searching method to use? 
What is the difference between structure and union?
Can we initialize unions?
Why can’t we compare structures?
How are structure passing and returning implemented?
What is a structure?
What are the differences between structures and union?
What is a union?
What the advantages of using Unions?
which one is better structure or union?(other than the space occupied )
How will you free the allocated memory?
What is the difference between UNION and UNION ALL?
Describe structures and Union in brief.
What are the properties of Union? 
What the advantages of using Unions?
Difference between structure and union?
What is a structure and a union in C?
 What are bit fields? What is the use of bit fields in a Structure declaration?
Explain with an example the self-referential structure.
what is the similarity between a Structure, Union and enumeration?
What are bit fields? What is the use of bit fields in a Structure declaration?
What the advantages of using Unions?
How can we read/write Structures from/to data files?
"union" Data Type What is the output of the following program? Why?
Which is the best sorting method?
What's the difference between these two declarations?