0% found this document useful (0 votes)
3 views20 pages

Structure Unit3

This document covers the concepts of arrays, structures, unions, typedef, and enums in C programming. It explains how structures can store different data types, how to access structure members, and the differences between structures and unions. Additionally, it provides examples of defining and using structures, nested structures, arrays of structures, and the use of typedef and enums for better code readability.

Uploaded by

wablesujit1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

Structure Unit3

This document covers the concepts of arrays, structures, unions, typedef, and enums in C programming. It explains how structures can store different data types, how to access structure members, and the differences between structures and unions. Additionally, it provides examples of defining and using structures, nested structures, arrays of structures, and the use of typedef and enums for better code readability.

Uploaded by

wablesujit1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIT-3

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;
}

• Here, p1, p2 and p3 are the variables of the structure 'student'.


CONTI…
• We can also declare structure variables at the time of defining structure as follows.
struct student
{
int roll_no;
char name[30];
int phone_number;
}p1, p2, p3;
Accessing Structure Members
There are two ways to access structure members:
 By . (member or dot operator)
 By -> (structure pointer operator)
 Now, let's see how to enter the details of each student i.e. roll_no, name and phone
number.
 Suppose, we want to assign a roll number to the first student. For that, we need to
access the roll number of the first student. We do this by writing
p1.roll_no = 1;
 This means that dot (.) is used to access the variables in a structure. p1.roll_no can
be understood as roll_no of p1.
Example:
// Create a structure called myStructure
struct myStructure {
int myNum; Output:
char myLetter;
char name[10];
}; My number: 14
My letter: S
int main() { My name: Sam
// Create a structure variable of myStructure called s1
struct myStructure s1;

// Assign values to members of s1


s1.myNum = 14;
s1.myLetter = ‘S';
strcpy(s1.name,”Sam”);

// 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

printf("\nEnter Rollno:"); Enter Rollno:5


scanf("%d",&st[i].rollno); Enter Name:Alex
printf("\nEnter Name:"); Student Information List:
scanf("%s",st[i].name); Rollno:1, Name:Sam
}
Rollno:2, Name:David
printf("\nStudent Information List:");
Rollno:3, Name:Raj
for(i=0;i<5;i++){
Rollno:4, Name:John
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
Rollno:5, Name:Alex
} return 0;
}
UNIONS
 Unions are quite similar to structures in C. Like structures, union is also a
user defined datatype.
union car
{
char name[50];
int price;
};
 Defining a union is as easy as replacing the keyword struct with the
keyword union.
DEFINING A UNION
 To define a union, you must use the union statement in the same way as we did while defining a structure. The
union statement defines a new data type with more than one member for our program.
union car
{
char name[50];
int price;
} car1, car2, car3;
OR
union car
{
char name[50];
int price;
};
int main()
{
union car car1, car2, car3;
return 0;
}
DIFFERENCE BETWEEN STRUCTURE AND UNION
1. MEMORY ALLOCATION
CONTI…
 More memory is allocated to structures than union
 The amount of memory required to store a structure variable is the sum of memory size
of all members.

 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

You might also like