UNIT V
STRUCTURES
Contents
Structures:
• Introduction
• Structures Vs Arrays
• Structure declaration
• Initialization of Structures
• Accessing the members of a Structure
Derived Data Types
Function Array Pointer Structure Union Enumerated
Type Type Type Type Type Type
Introduction
• A structure is a collection of related elements,
possibly of different types, having a single name.
• A structure is a user-defined data type that can store
related information about an entity.
• A structure is a composite data type which contains
declarations for several items called members. The
members of a structure can have any data type in C,
including data type struct.
• The composition of a structure is declared as
struct structName {
data_type member1;
data_type member2;
……
};
• For ex.
struct student {
int r_No;
char name[20];
char course[20];
float fees;
int gradePts;
};
• Each element in a structure is called a field or a
member.
A field is the smallest element of named data that has
meaning.
A field has many of the characteristics of the variables.
Every field has a type , and it exists in memory.
It can be assigned values, which can be accessed for
selection or manipulation.
• A field differs from a variable primarily in that it is part
of a structure.
Structures Vs Arrays
• Array is also a derived data type that can hold multiple
pieces of data.
• The difference between an array and a structure is that
all the elements in an array are of the same type, while
the elements in a structure can be of different types.
• All elements in an array must have same data type ie
an array must consist of homogeneous data. On the
other hand, a structure allows grouping of data values
which are heterogeneous.
• Examples of structure
Student 🡪 Id, Name, gradePoints
Address 🡪 streetname , housenumber
• The data in a structure should be related to one
object.
• A structure is a pattern or outline that can be applied
to data to extract individual parts.
• A structure allows us to refer to a collection of data
using a single name and, at the same time , to refer to
the individual components through their names.
• We can collect all attributes of an object in one
structure. This simplifies our program and makes them
more readable.
Structure declaration ie the composition of a structure is
defined as
struct address {
int houseNumb;
char streetName[10];
};
• A structure declaration does not set up any storage. It
defines a template or pattern for a variable ie
structure declaration defines a type.
• Declaring the variables is done as follows.
struct address home_address, work_address;
• Defining the structure and using it to declare a variable
can be combined as follows.
struct address {
int houseNumb;
char streetName[10];
} home_address, work_address;
• Another Ex. A structure variable can either be declared with
structure declaration or as a separate declaration like basic
types.
struct student { int r_No;
char name[20];
char course[20];
float fees;
int gradePts;
} s1,s2;
OR
struct student { int r_No;
char name[20];
char course[20];
float fees;
int gradePts;
};
struct student s1,s2;
• The members of a structure are usually processed
individually, as separate entities.
• A member of a structure is identified and accessed
using the dot operator (.).
The dot operator connects the member name to the
name of its containing stucture.
[Link]
For ex.
home_address.houseNumb=123;
// Illustration of structures
#include <stdio.h>
int main()
{
struct student {
char name[40];
double average;
char grade;
} s2;
// Initialisation of a structure variable
struct student s1={"Sam",2.0, 'C'};
[Link]=5.6;
[Link] = 'B';
printf("\n Name: %s", [Link]);
return 0; }
Output:
Name: Sam Grade:C
//Initialization of structure variables
# include<stdio.h>
int main()
{
struct book
{ char name[20];
float price;
int pages;}; //Declaration
//Initialization of structures
struct book b1={"Basic Electronics", 450.00,560};
struct book b2={"Physics", 350.00, 300};
struct book b3={0};
// Accessing structure members
printf("\n%s\t %5.2f\t \t%d\n ",[Link],[Link],[Link]);
printf("\n%s\t %5.2f\t \t%d\n ",[Link],[Link],[Link]);
printf("\n%s\t %5.2f\t \t%d\n ",[Link],[Link],[Link]);
return 0;}
Output:
Basic Electronics 450.00 560
Physics 350.00 300
0.00 0
//print the details of 3 books using structures
# include<stdio.h>
int main()
{ struct book { char name;
float price;
int pages; };
struct book b1,b2,b3;
printf("Enter names, prices and pages of 3 books");
// accessing members of a structure
scanf(" %c %f %d", &[Link],&[Link], &[Link]);
scanf(" %c %f %d", &[Link],&[Link], &[Link]);
scanf(" %c %f %d", &[Link],&[Link], &[Link]);
printf("\n The details of the books are as follows.\n");
printf("\n %c %5.2f %d", [Link],[Link],[Link]);
printf("\n %c %5.2f %d", [Link],[Link],[Link]);
printf("\n %c %5.2f %d", [Link],[Link],[Link]);
return 0;
}
Output:
Enter names, prices and pages of 3 books
A 67.50 89
B 56.3 70
C 78.00 80
The details of the books are as follows.
A 67.50 89
B 56.30 70
C 78.00 80