Module 5
Module 5
Module 5
By Asst. Prof. Sudeshna Baliarsingh
Information & Technology Dept.
KJSIT, Mumbai
Structure and Union
Entity type
A person, organization, object type, or concept about which information is
stored. Describes the type of the information that is being mastered. An entity
type typically corresponds to one or several related tables in database.
Attribute
A characteristic or trait of an entity type that describes the entity, for example,
the Person entity type has the Date of Birth attribute.
Why use structure?
In C, there are cases where we need to store multiple attributes of an entity. It is not
necessary that an entity has all the information of one type only.
For example, an entity Student may have its name (string), roll number (int), marks
(float). To store such type of information regarding an entity student, we have the
following approaches:
● Construct individual arrays for storing names, roll numbers, and marks.
● Use a special data structure to store the collection of different data types.
Before you can create structure variables, you need to define its data type. To define a struct, the struct
keyword is used.The struct define a new data type which is collection of different type of data.
C Structure Definition
In both cases,
Method 1 Method 2
Memory allocation in structure:
Access Members of a Structure
There are two types of operators used for accessing members of a structure.
. - Member operator
This initializes person1 with the name "Alice Smith" and age 25 directly during declaration.
Copying Structures
You can copy the contents of one structure to another by using the assignment operator =:
This copies the values of person1 (name and age) into person2.
So, if we declare the variable this way, it’s not possible to handle this.
For that, we can define an array whose data type will be struct Employee soo that will be
easily manageable.
In the main() function, the program populates the contacts array with
contact information by using the strcpy() function to copy strings into the
name and phoneNumber members of each contact.
After populating the contact list, the program iterates over the contacts array
using a for loop and prints out the information for each contact using printf().
It prints the name and phone number for each contact, separating each
contact's information with a newline character.
Write a program to read Title, author and price of 10 books using an array of
structure and Display the record.
wAP IN C ApplYING structure to display employee id
NAME AND WEEK ATTENDANCE
Output
Nested Structure in C
A Basic Example:
Ways in Which a Structure Can Be Nested
1. By Embedded Structure
As the name suggests, this method is used to embed one structure inside
another, i.e defining one structure in the definition of another structure.
Thus using this method, the nested structure will be declared inside
the parent structure.
2. By Separate Structure
The two structures are created separately in this method. The Dependent
structure is used inside the Main/Parent structure by taking a member
of the dependent structure type in the definition of the parent
structure.
Embedded structure follows Example
the below format:
Nested Structures in C can be initialized at the time of declaration. There are two methods by
which we can initialize nested structures:
Method 1 : The embed structure is initialized first using the structure variable of that
structure, then the parent structure is initialized next using that already initialized member of
the embed structure.
In this program, employee is
our parent structure and
incomeInLPA is our
nested/embed structure.
We define a function named display which takes a pointer to a struct student as its argument. This function will
display the details of a student.
Inside the display function, we use printf to print the name of the student. We access the name member of the
student structure using the arrow operator (->) since student_obj is a pointer to a structure.Similarly, we print the
roll number of the student by accessing the roll member of the structure pointed to by student_obj.
Finally, we print the marks of the student by accessing the marks member of the structure pointed to by
student_obj.In the main function, we declare an instance of the student structure named st1 and initialize it with
the values "Aman" for name, 19 for roll, and 8.5 for marks.
We call the display function, passing the address of the st1 structure as an argument. This means we're passing a
reference to st1, allowing the display function to directly access and modify the original structure.
C typedef
The typedef is a keyword that is used to provide existing data types with a new
name. The C typedef keyword is used to redefine the name of already existing data
types.
When names of datatypes become difficult to use in programs, typedef is used with
user-defined datatypes, which behave similarly to defining an alias for commands.
It can be used with structures to increase code readability and we don’t have to type
struct repeatedly.
Example : Using typedef to define a name for a structure
Output
Name: Kamlesh Joshi
Branch: Computer
Science And
Engineering
ID_no: 108
C Unions
The Union is a user-defined data type in C language that can contain elements of the different
data types just like structure. But unlike structures, all the members in the C union are stored
in the same memory location. Due to this, only one member can store data at the given
instance.
The syntax to declare a union is with the union keyword, similar to the way struct is used for
structures.
Syntax
This implies that although a
union may contain many
members of different types,
it cannot handle all the
members at the same time.
C Union Declaration
This declares a variable circle1 and circle2 of type union circle. This union contains two
members each with a different data type. However only one of them can be used at a
time. This is due to the fact that only one location is allocated for all the union variables,
irrespective of their size. The compiler allocates the storage that is large enough to hold
the largest variable type in the union.
Different Ways to Define a Union Variable
When you declare a union, you can simultaneously define union variables:
To access the members of a union, you use the dot (.) operator. If you have a pointer to a union, then you'd use the
arrow (->) operator.
Example of Union
A real-world example is a
two-wheeler dealership that
has both bicycles and
motorcycles. While a
motorcycle might have
attributes like engine size
and mileage, a bicycle might
only have attributes like color.
However, both might have a
price attribute. Using unions,
the memory space for these
attributes can be optimized:
Discuss the
implementation of a
structure with a union in
C programming.