Structure
What is Structure
a ‘book’ is a collection of things such as title, author, call number, publisher, number of pages,
date of publication, etc
all this data is dissimilar
author is a string, whereas number of pages is an integer
What is Structure
For dealing with such collections, C provides a data type called ‘structure’
A structure gathers together, different atoms of information that comprise a given entity
We have seen earlier how ordinary variables can hold one piece of information
how arrays can hold a number of pieces of information of the same data type
What is Structure
These two data types can handle a great variety of situations
But quite often we deal with entities that are collection of dissimilar data types
What is structure
to store data about a book. You might want to store
1. its name (a string)
2. its price (a float)
3. number of pages in it (an int)
Memory Allocation
What is Structure
we can follow two approaches
1. Construct individual arrays, one for storing names, another for storing prices and still
another for storing number of pages.
2. Use a structure variable.
What is structure
Let us examine these two approaches one by one
Making things with
Arrays
Making things with
Structures
Declaration of Structure and
Structure Variables
The general format of structure definition is:
In can be also written as--
Initializing Structure
Variables
Accessing Structure
Elements
Array of Structures
Memory Allocation
Nested Structure
C provides us the feature of nesting one structure within another structure by using which, complex data
types are created. For example, we may need to store the address of an entity employee in a structure.
The attribute address may also have the subparts as street number, city, state, and pin code. Hence, to
store the address of the employee, we need to store the address of the employee into a separate
structure and nest the structure address into the structure employee. Consider the following program.
Nested Structure
Union
Union can be defined as a user-defined data type which is a collection of different variables of different
data types in the same memory location.
The union can also be defined as many members, but only one member can contain a value at a particular
point in time.
Union is a user-defined data type, but unlike structures, they share the same memory location.
Union
In the above code, union has two members, i.e., 'a' and 'b'. The 'var' is a variable of union abc type. In
the main() method, we assign the 66 to 'a' variable, so var.a will print 66 on the screen. Since both 'a' and 'b'
share the memory location, var.b will print 'B' (ascii code of 66).
Why Union?
In the above code, we have created a variable of type store. We have
assigned the values to the variables, title, author, number_pages, price
but the book variable does not possess the properties such as size, color,
and design. Hence, it's a wastage of memory. The size of the above
structure would be 32 bytes.
Why Union?
We can save lots of space if we use unions.
In the above code, we have created a variable of type store. Since we used the unions in the
above code, so the largest memory occupied by the variable would be considered for the
memory allocation. The output of the above program is 20 bytes. In the case of structures,
we obtained 32 bytes, while in the case of unions, the size obtained is 32 bytes. Hence, 32
bytes is greater than 20 bytes saving lots of memory space.