Structure and Union
Structure and Union
5
Compiled By: Ravi Singh
INITIALIZING STRUCTURE MEMBERS
▪ Structure members can be initialized at declaration.
▪ This much the same manner as the element of an array; the initial value must appear in the
order in which they will be assigned to their corresponding structure members, enclosed in
braces and separated by commas.
Example:
11
Compiled By: Ravi Singh
ARRAY OF STRUCTURE
▪ In C programming, the combination of arrays and structures i.e. array of structures provides a
powerful tool for managing that.
▪ An array whose elements are of type structure is called array of structure. It is generally useful
when we need multiple structure variables in our program.
Need for array of structure
Suppose we have 50 employees and we need to store the data of 50 employees. So for that, we
need to define 50 variables of struct Employee type and store the data within that. However,
declaring and handling the 50 variables is not an easy task. Let’s imagine a bigger scenario, like
1000 employees.
12
Compiled By: Ravi Singh
ARRAY OF STRUCTURE
Declaration
struct struct_name
{
type element 1;
type element 2;
……………..
type element n;
}array name[size];
Example
struct student
{
int rollno;
char name[25];
float totalmark;
} stud[100]; 13
Compiled By: Ravi Singh
STRUCTURE AND FUNCTION
▪ Like an ordinary variable, a structure
variable can also be passed to a function.
▪ We may either pass individual structure
elements or the entire structure.
Passing structure members to functions
▪ A structure member can be treated just as
any other variable of that type.
▪ For example, an integer member of structure
can be treated just as an integer.
▪ Thus, structure members can be passed to
function like ordinary variables.
▪ Example program to illustrate passing
structure members to a function :
14
Compiled By: Ravi Singh
STRUCTURE AND FUNCTION
Passing whole structure to a functions
15
Compiled By: Ravi Singh
STRUCTURE AND FUNCTION
Passing array of structure to a function
It is possible to send entire array of structures
to functions as argument in the function call.
Example
A program to illustrate passing whole
structure to function.
16
Compiled By: Ravi Singh
POINTER TO STRUCTURE
Pointers can be used also with structure. To store address of a structure
type arable, we can define a structure type pointer variable as normal
way. Let us consider structure book that has members name, page and
price. It can be declared as
struct book
{
char name[10];
int page;
float price;
};
struct book b;
▪ An individual structure member can be accessed in terms of its corresponding pointer variable by
writing
ptr_variable->member
▪ Here, -> is called arrow operator and there must be pointer to the structure on the left side of this
operator.
▪ i.e.Now, the members name, pages and price of book can be accessed as
▪ An individual structure member can be accessed in terms of its corresponding pointer variable by
writing
ptr_variable->member
▪ Here, -> is called arrow operator and there must be pointer to the structure on the left side of this
operator.
▪ i.e.Now, the members name, pages and price of book can be accessed as
20
Compiled By: Ravi Singh
TYPEDEF
▪ In C programming, the typedef keyword is used to provide existing data types
with new names.
Creating an Alias for Data Types:
▪ You can use typedef to redefine the name of an existing data type. This is helpful
when the original type name becomes cumbersome or less intuitive.
▪ The syntax for typedef is:
typedef existing_name alias_name;
▪ After this declaration, you can use alias name as if it were the real existing_name
in your C program.
▪ In above example, structure members a and b have different addresses where as union members a
and b have same addresses. It means the union members a and b are storing same memory
locations.
Compiled By: Ravi Singh 23
UNION DECLARATION
A union is declared in the same way as a For example, the following code declares a union
structure. The syntax of union declaration is data type called Student and a
union union_name union variable called stud:
{ union student
type element 1; {
type element 2; int rollno;
................. F loat totalmark;
type element n; };
}; union student stud;
This declares a type template. Variables are
then declared as:
union union_name x,y,z;
Memory Allocation Allocates memory for all its Allocates memory only for the
members. largest member.
Total Size Sum of sizes of all members. Size of the largest member.
Data Storage Can store values for all members Can store value for only one
simultaneously. member at a time.
Use Case When you want to group different When you want to save memory
data types and access all of them at and only need one data type at a
once. time.
Example struct example { union example {
int a; int a;
float b; float b;
} }
Accessing Members All members can be accessed at any Only the last stored member can
time. be accessed.
Modification Impact Modifying a member doesn’t affect Modifying one member may
Compiled By: Ravi Singh 27
other members. overwrite other members.
Compiled By: Ravi Singh 28