PPS - Unit 8
PPS - Unit 8
Structure
Syllabus Topics
✓ Basics of structure
✓ structure members
✓ nested structures
✓ array of structures
Data types in C
✓ C language has built-in datatypes like primary and derived data types.
✓ But, still not all real world problems can be solved using those data types.
• Example-2: Student
▪ Structure helps to build a complex datatype which is more meaningful than an array.
▪ An array holds elements of similar datatype, while structure holds elements of different
datatypes.
• This keyword is reserved word in C language. We can only use it for structure and its object declaration.
Syntax
struct student
{
char name[30];
int roll_no;
float CPI;
int backlog;
};
• You cannot assign value to members inside the structure definition, it will cause
compilation error.
Create Structure Variable
• Hence, let us learn how to create our custom structure type objects also known
as structure variable.
Syntax Example
struct structure_name struct student
{ {
member1_declaration; char name[30];
member2_declaration; int roll_no;
. . . float CPI;
memberN_declaration; int backlog;
} structure_variable; } student1;
Create Structure Variable
2) Declaration after Structure definition
Syntax
struct StructureName StructureVariable;
Example
struct student
{
char name[30];
int roll_no;
float CPI;
int backlog;
};
1)
2)
#include<stdio.h>
struct Student
{ Write a program to read and display
char Name[20]; student information using structure.
int Roll_no;
float Marks;
}S1;
void main()
{
printf(" Enter Name : ");
scanf("%s", S1.Name);
printf("\n --------- Student Details --------- \n"); --------- Student Details ---------
Syntax Example
struct student
struct structure_name {
{ char name[30];
member1_declaration; int roll_no;
member2_declaration; float CPI;
. . . int backlog;
memberN_declaration;
} S[5];
} structure_variable[size];
#include<stdio.h>
struct Student
{
char Name[20]; Write a program
int Roll_no;
float Marks;
to read and
}S[3]; display student
information for 3
void main() students using
{ int i;
array of
for(i=0;i<3;i++) structure.
{
printf("\n ---------- Enter Details of Student %d ---------- \n",i+1);
Yash 1 99.000000
Prisha 2 95.000000
Aarav 3 97.000000
Structure and pointers
• Address of structure variable is stored in a pointer of Structure type.
• Using -> operator, we can access structure members using structure pointer.
Syntax Example
struct student
struct structure_name {
{ char name[30];
member1_declaration; int roll_no;
member2_declaration; float CPI;
. . . int backlog;
memberN_declaration;
} S, *p;
} Structure_Variable, *Pointer_Name;
Program
#include<stdio.h>
struct Student Write a program to access
{ Structure Members using a
char Name[20]; Structure Pointer
int Roll_no;
float Marks;
}S1,*p;
void main()
{
p = &S1;
printf(" Enter Name : ");
scanf("%s",p->Name);
printf(" Enter Roll_no : ");
scanf("%d",&p->Roll_no ); Output
printf(" Enter Marks : "); Enter Name : Smit
scanf("%f", &p->Marks ); Enter Roll_no : 1
Enter Marks : 65
printf("\n --------- Student Details --------- \n");
--------- Student Details ---------
printf("\n Student Name = %s ",p->Name);
printf("\n Student Roll_No = %d ",p->Roll_no); Student Name = Smit
printf("\n Student marks = %f ",p->Marks); Student Roll_No = 1
} Student marks = 65.000000
Nested Structures
struct structure_name1
{
Example
member1_declaration;
member2_declaration; struct student
. . . {
memberN_declaration; char name[30];
int roll_no;
struct structure_name2 float CPI;
{ int backlog;
// member_declaration;
} S, *p;
} Inner_Structure_Variable;
} Outer_Structure_Variable;
Union
▪ The concept of union is borrowed from structure, but both differ in the terms of
memory storage.
▪ This means that in union at a time only one member can be accessed.
▪ The compiler allocates the storage that is large enough to hold the largest data type
among the union members.
Enumeration
▪ Enum is a keyword.
enum Weekdays
▪ “It is used to define more than one
{
integer symbolic constants”. Sunday,
▪ Compiler automatically assigns integer Monday,
digits beginning with 0 and remaining Tuesday,