Structure Unit3
Structure Unit3
ARRAYS
AND
STRUCTURES
Structures
Accessing structure members
Nested structure
Array of Structure
SYLLABUS
Unions
Difference between structure
and union
Typedef
enum
STRUCTURE
• Arrays are used to store similar type of data. Have you ever thought if there is any
way to store dissimilar data?
• Structures (also called structs) are a way to group several related variables into one
place. Each variable in the structure is known as a member of the structure.
Structure is a user defined datatype.
• Unlike an array, a structure can contain many different data types (int, float, char,
etc).
• For example: A student can have various records . Student’s name is a string and
the phone number and roll_no are integers. So, here name, address and phone
number are different types of data. Here, structure comes in picture.
• The collection of all related information under a single name Student is a structure.
DEFINING A STRUCTURE
• Keyword struct is used for creating a So, our structure will look as below:
structure.
struct employee
• The syntax for structure is:
{
struct structure_name
int id;
{
data-type member-1; char name[10];
data-type member-2; float salary;
data-type member-3; };
data-type member-4;
};
• In our case, let's name the structure as
employee. The members of the structure in
our case are id, name and salary.
CONTI…
Here, struct is the
keyword. employee is the name of
the structure. id, name,
and salary are the members or fields
of the structure. Let's understand it
by the diagram given below:
[10] ;
Declaration of Structure Variable
• Just as we declare variables of type int, char etc, we can declare variables of structure as well.
• Suppose, we want to store the roll no, name and phone number of three students. For this, we
will define a structure of name 'student' and then declare three variables, say 'p1', 'p2' and 'p3'
(which will represent the three students respectively) of the structure 'student'.
struct student
{
int roll_no;
char name[30];
int phone_number;
};
main()
{
struct student p1, p2, p3;
}
// Print values
printf("My number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);
printf("My name: %s\n", s1.name);
return 0;
}
Nested structure
A nested structure in C is a structure within structure.
Syntax:
struct name_1
{
member1;
member2;
.
.
membern;
struct name_2
{
member_1;
member_2;
.
.
member_n;
}, var1
} var2;
• The member of a nested structure can be accessed using the following syntax:
Variable name of Outer_Structure.Variable name of Nested_Structure.data member to access
Example: org.emp.employee_id;
Array of structures
An array of structures in C can be defined as the collection of multiple
structures variables where each variable contains information about
different entities.
The array of structures in C are used to store information about multiple
entities of different data types.
The array of structures is also known as the collection of structures.
EXAMPLE:
#include<stdio.h> Enter Records of 5 students:
#include<string.h> Enter Rollno:1
struct student{ Enter Name:Sam
int rollno; Enter Rollno:2
char name[10]; };
Enter Name:David
int main(){
Enter Rollno:3
int i;
Enter Name:Raj
struct student st[5];
Enter Rollno:4
printf("Enter Records of 5 students:");
for(i=0;i<5;i++){ Enter Name:John
Unlike, the memory required to store a union variable is the memory required for the
largest element of an union.
CONTI…
2. ACCESSING MEMBERS
Only one union member can be accessed at a time
In the case of structure, all of its members can be accessed at any time.
But, in the case of union, only one of its members can be accessed at a time and all
other members will contain garbage values.
Structure Union
We use the struct statement to define a structure We use the union keyword to define a union
Every member is assigned a unique memory All the data members share a memory location
location
Change in the value of one data member does not Change in the value of one data member affects the value
affect other data members in the structure of other data members.
You can initialize multiple members at a time You can initialize only the first member at once
A structure can store multiple values of the different A union stores one value at a time for all of its members
members
A structure’s total size is the sum of the size of A union’s total size is the size of the largest data member
every data member
Users can access or retrieve any member at a time You can access or retrieve only one member at a time
TYPEDEF IN C PROGRAMMING
The typedef is a keyword used in C programming to provide some meaningful names to the already
existing variable in the C program.
Syntax of typedef :
typedef <existing_name> <alias_name>
For example, suppose we want to create a variable of type unsigned int, then it becomes a tedious
task if we want to declare multiple variables of this type. To overcome the problem, we use a
typedef keyword.
typedef unsigned int unit;
In the above statements, we have declared the unit variable of type unsigned int by using a
typedef keyword.
Now, we can create the variables of type unsigned int by writing the following statement:
unit a, b;
instead of writing:
unsigned int a, b;
ENUMERATION IN C PROGRAMMING
The enum in C is also known as the enumerated type. It is a user-defined data type that consists of integer
values, and it provides meaningful names to these values. The use of enum in C makes the program easy to
understand and maintain. The enum is defined by using the enum keyword.
The following is the way to define the enum in C:
enum enum_name{integer_const1, integer_const2,.....integer_constN};
Example:
#include <stdio.h>
enum weekdays{Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
int main()
{
enum weekdays w; // variable declaration of weekdays type
w=Monday; // assigning value of Monday to w.
printf("The value of w is %d“,w);
return 0;
}
Output:
The value of w is 2