SlideShare una empresa de Scribd logo
1 de 18
Descargar para leer sin conexión
@2020 Presented By Y. N. D. Aravind 1
Presented By
Y. N. D. ARAVIND
M.Tech, Dept of CSE
Newton’s Group Of Institutions, Macherla
Session Objectives
Unions
Typedef
Enumerated Data Type
@2020 Presented By Y. N. D. Aravind
2
2
Structures
A Structure can be defined to be a group of logically related data items,
which may be of different types, stored in contiguous memory locations,
sharing a common name, but distinguished by its members.
The decleration of a structure specifies the grouping of various data items into a
single unit without assigning any resources to them.
The syntax for declaring a structure in c is as follows
struct TAG
{
Data Type varaible-1;
Data Type variable-2;
……
……
Data Type variable-n;
};
Where,
 struct is the keyword which tells the compiler that a structure is being defined.
 Tag_name is the name of the structure.
 variable1, variable2 … are called members of the structure.
 The members are declared within curly braces.
 The closing brace must end with the semicolon.
@2020 Presented By Y. N. D. Aravind
3
3
Example :
struct employee
{
int eno;
char name[80];
float sal;
};
Structure Definition
The decleration of a structure will not serve any purpose without its definition.
It only acts as a bluprint for the creation of variables of type struct. The
structure definition creates structure variable and allocates storage space for
them.
Method 1 : struct Structurename var1, var2, . . . . . ;
Example : struct employee x;
Example : struct employee x, y, z ;
Method 2 :
@2020 Presented By Y. N. D. Aravind
4
4
Example :
struct employee
{
int eno;
char name[80];
float sal;
};
2 bytes
80 bytes
4 bytes
Example :
struct employee
{
int eno;
char name[80];
float sal;
}x;
The structure variable can be
created during the decleration
as follows
Accessing Structure Members
@2020 Presented By Y. N. D. Aravind
5
The members of a structure can be accessed by using dot(.) operator.
dot (.) operator
Structures use a dot (.) operator(also called period operator or member operator) to
refer its elements. Before dot, there must always be a structure variable. After the dot,
there must always be a structure element.
syntax :- structure_variable_name . structure_member_name
struct student
{
char name [5];
int roll_number;
float avg;
};
struct student s1;
The members can be accessed using the variables as shown below,
s1.name --> refers the string name
s1.roll_number --> refers the roll_number
s1.avg --> refers avg
5
Structure Initilization
@2020 Presented By Y. N. D. Aravind
6
Like any other data types, structure variables can be initialized at the point of their
definition.
Consider the following structure decleration.
struct student
{
char name [5];
int roll_number;
float avg;
};
Example :-
A variable of the structure employee can be
initialized during its defination as folloes:
struct student s1 = {“Ravi”, 10, 67.8};
6
name
roll no
avg
Array of Structures
@2020 Presented By Y. N. D. Aravind
7
An array is a collection of elements of same data type that are stored in contiguous
memory locations. A structure is a collection of members of different data types stored in
contiguous memory locations. An array of structures is an array in which each element is
a structure. This concept is very helpful in representing multiple records of a file, where
each record is a collection of dissimilar data items.
As we have an array of integers, we can have an array of structures also. For example,
suppose we want to store the information of class of students, consisting of roll_number,
name and percentage, A better approach would be to use an array of structures.
Array of structures can be declared as follows, struct tag_name arrayofstructure[size];
Let‟s take an example, to store the information of 20 students, we can have the following
structure definition and declaration,
struct student
{
int rollno;
char name[80];
float per;
};
struct student s[20];
7
Program to illustrate an array of structures
@2020 Presented By Y. N. D. Aravind
8
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[80];
float per;
};
void main( )
{
struct student s[20];
int i,n;
clrscr();
printf("Enter no of students [ 1 – 20 ] n ");
scanf(“%d”,&n);
printf("Enter %d students details n ");
for(i = 0; i < n; i++)
scanf(“%d %s %f”,&s[i].rollno,s[i].name,&s[i].per);
printf("n Student details: n");
for(i=0; i<n; i++)
printf(“%3d %20s %9.2f n”, s[i].rollno,s[i].name,s[i].per);
getch(();
}
8
Input-Output
Enter no of students [ 1 – 20 ]
2
Enter 2 students details
1 Ramesh 90
2 Anusha 80
Student details:
1 Ramesh 90.00
2 Anusha 80 .00
Array Within Structures
@2020 Presented By Y. N. D. Aravind
9
It is also possible to declare an array as a member of structure, like declaring ordinary
variables. For example to store marks of a student in six subjects then we can have the
following definition of a structure.
Example :-
struct student
{
int rollno;
char name[80];
int marks[6];
};
Then the initialization of the array marks done as follows,
struct student s1= {“ravi”, 34, {60,70,80,68,90,75}};
The values of the member marks array are referred as follows,
s1.marks [0] --> will refer the 0th element in the marks 60
s1.marks [1] --> will refer the 1st element in the marks 70
s1.marks [2] --> will refer the 2nd element in the marks 80
s1.marks [3] --> will refer the 3rd element in the marks 68
s1.marks [4] --> will refer the 4th element in the marks 90
s1.marks [5] --> will refer the 5th element in the marks 75
9
To create a list of students details and display them.
@2020 Presented By Y. N. D. Aravind 10
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[80];
int marks[6];
int sum;
float per;
};
void main( )
{
struct student x[20];
int i,n,j;
clrscr();
printf("Enter no of students [ 1 – 20 ] n ");
scanf(“%d”,&n);
for(i = 0; i < n; i++)
{
printf("Enter rollno , name of students - %d n “,i+1);
scanf(“%d %s ”,&x[i].rollno,x[i].name);
Printf(“ Enter marks in six subjects n”):
for(j = 0; j < 6; j++)
scanf(“%d”, &x[i].marks[j]);
}
for(i = 0; i < n; i++)
{
x[i].sum = 0;
for(j = 0; j < 6; j++)
x[i].sum += x[i].marks[j];
x[i].per = (float) x[i].sum / 6;
}
printf(“ Roll no t Name t Percentage n”);
for(i=0; i < n; i++)
printf(“%3f %20s %9.2f n”,x[i].rollno,x[i].name,
x[i].per);
getch();
}
Input – Output
Enter no of students [ 1 – 20 ]
2
Enter rollno , name of students –1
1 Ramesh
Enter marks in six subjects
68 79 77 60 91 75
Enter rollno , name of students – 2
2 Anusha
Enter marks in six subjects
88 68 66 60 90 74
Roll no Name Percentage
1 Ramesh 75.00
2 Anusha 74.40
Structure Within Structure
@2020 Presented By Y. N. D. Aravind
11
A structure which includes another structure is called nested structure or structure within structure.
i.e a structure can be used as a member of another structure. There are two methods for declaration
of nested structures.
11
1. The syntax for the nesting of the structure is as
follows
struct tag_name1
{
type1 member1;
…….
…….
};
struct tag_name2
{
type1 member1;
……
……
struct tag_name1 var;
……
};
The syntax for accessing members of a nested
structure as follows,
outer_structure_variable .
inner_structure_variable . member_name
2. The syntax of another method for the nesting
of the structure is as follows
struct structure_nm
{
<data-type> element 1;
<data-type> element 2;
- - - - - - - - - - -
- - - - - - - - - - -
<data-type> element n;
struct structure_nm
{
<data-type> element 1;
<data-type> element 2;
- - - - - - - - - - -
- - - - - - - - - - -
<data-type> element n;
}inner_struct_var;
}outer_struct_var;
To illustrate the concept of structure within structure.
@2020 Presented By Y. N. D. Aravind 12
#include<stdio.h>
#include<conio.h>
Struct date
{
int day;
int month;
int year;
};
struct student
{
int rollno;
char name[80];
float per;
struct date doa;
};
void main( )
{
struct student x;
clrscr();
printf("Enter Roll no Name DOB and Per n ");
scanf(“%d %s %d %d %d %f”,&x.rollno,
x.name,&x.doa.day, &x.doa.month,
&x.doa.year,&x.per);
printf(“ Roll no : %d n”,x.rollno);
printf(“ Name : %s n”,x.name);
printf(“ Percentage =:%3.2f n”,x.per);
printf(“ DOB : %d / %d /%d n”,
x.doa.day,x.doa.month,x.doa.year);
getch();
}
Input – Output
Enter Rollno Name DOB and Per
1 Ramesh 20 10 2008 90
Roll no : 1
Name : Ramesh
Percentage : 90.00
DOB : 20/10/2008
Self Referential Structure
@2020 Presented By Y. N. D. Aravind
13
A structure contaning a pointer to the same type is referred to as self- referential structure. It is
useful for implementing data structures such as linked list, queues, stacks and trees etc.. A linked list
consists of structures related to each other through pointers. The self referential pointer in the
structure points to the next node of a list.
13
Syntax:-
struct tag_name
{
Type1 member1;
Type2 member2;
……….
struct tag_name *next;
};
Ex:-struct node
{
int data; node
struct node *next;
} n1, n2;
data next
UNION
@2020 Presented By Y. N. D. Aravind
14
A union is one of the derived data types. Union is a collection of variables referred under a single
name. The syntax, declaration and use of union is similar to the structure but its functionality is
different
14
Syntax:-
Union Union_name
{
Type1 member1;
Type2 member2;
……….
............
Typen membern;
}
Union is a keyword , Union name is any user defined name which should be a valid C identifier.Data
type is any valid data type supported by C or user-defined type. Member-1, member-2, ....,member-n
are the members of union.
The syntax of declaring a variable of union type is
Syntax:-
Union Union_name var1,var2,.......................
ENUMERATED DATA TYPE
@2020 Presented By Y. N. D. Aravind
15
Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names
to integral constants, the names make a program easy to read and maintain.
The syntax of defining an enumerated type is as follows:
The syntax of declaring a variable of enum type is similar to that used while declaring
variables of structure or union type
Example :-
enum boolean{true, false};
enum boolean t,f;
t= true;
f= false;
The compiler automatically assigns integer digits begining with 0 to all the enumerated
constants. „i.e‟ the enumerated constant true is assigned 0, false is assigned 1
15
Syntax:-
enum identifier{ value-1,varlue-2,......................value-n};
Syntax:-
enum identifier v1,v2,......................vn;
ENUMERATED DATA TYPE
@2020 Presented By Y. N. D. Aravind
16
However, the automatically assignments can be overridden by assigning values
explicitly to the enumerated constants.
enum State {Working = 1, Failed};
Here, the constant working is assigned the value of 1, the remaning constants are
assigned values that increase successively by 1.
16
#include<stdio.h>
#include<conio.h>
void main( )
{
enum month{ Jan, Feb, Mar, Apr, May, June, July, Aug, Sep, Oct, Nov, Dec};
enum month year_st, year_end;
clrscr();
year_st = Jan;
year_end = Dec;
printf(“Jan = %d n”, year_st);
printf(“ Feb = %d n “, year_end;
getch();
}
Input –Output
Jan = 0
Dec =11
Typedef
@2020 Presented By Y. N. D. Aravind
17
typedef is a keyword used in C language to assign alternative names to existing
datatypes. Its mostly used with user defined datatypes, when names of the datatypes
become slightly complicated to use in programs. Following is the general syntax for
using typedef,
typedef <existing_name> <alias_name>;
Lets take an example and see how typedef actually works.
typedef unsigned long ulong;
The above statement define a term ulong for an unsigned long datatype. Now
this ulong identifier can be used to define unsigned long type variables.
ulong i,
typedef can be used to give a name to user defined data type as well. Lets see its use with
structures.
typedef struct
{ type member1;
type member2;
type member3;
} type_name;
Here type_name represents the stucture definition associated with it. Now
this type_name can be used to declare a variable of this stucture type.
type_name t1, t2;
17
Thank You
@2020 Presented By Y. N. D. Aravind
18
Presented By
Y. N. D. ARAVIND
M.Tech, Dept of CSE
Newton’s Group Of Institutions, Macherla

Más contenido relacionado

La actualidad más candente

structure and union
structure and unionstructure and union
structure and union
student
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
MomenMostafa
 
6. Integrity and Security in DBMS
6. Integrity and Security in DBMS6. Integrity and Security in DBMS
6. Integrity and Security in DBMS
koolkampus
 

La actualidad más candente (20)

Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
User defined functions in matlab
User defined functions in  matlabUser defined functions in  matlab
User defined functions in matlab
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
structure and union
structure and unionstructure and union
structure and union
 
Database Management System-session 3-4-5
Database Management System-session 3-4-5Database Management System-session 3-4-5
Database Management System-session 3-4-5
 
computer notes - Reference variables
computer notes -  Reference variablescomputer notes -  Reference variables
computer notes - Reference variables
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Structures
StructuresStructures
Structures
 
Handout#06
Handout#06Handout#06
Handout#06
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
 
6. Integrity and Security in DBMS
6. Integrity and Security in DBMS6. Integrity and Security in DBMS
6. Integrity and Security in DBMS
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
 
Ch7 structures
Ch7 structuresCh7 structures
Ch7 structures
 

Similar a Structure In C

data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
patcha535
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
mrecedu
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
Rajeshkumar Reddy
 

Similar a Structure In C (20)

Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
C structure and union
C structure and unionC structure and union
C structure and union
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Chap 10(structure and unions)
Chap 10(structure and unions)Chap 10(structure and unions)
Chap 10(structure and unions)
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Structures
StructuresStructures
Structures
 
Structures
StructuresStructures
Structures
 
#Jai c presentation
#Jai c presentation#Jai c presentation
#Jai c presentation
 
CP Handout#10
CP Handout#10CP Handout#10
CP Handout#10
 
Ocs752 unit 5
Ocs752   unit 5Ocs752   unit 5
Ocs752 unit 5
 
Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
 
Structures
StructuresStructures
Structures
 
Structure for cpu
Structure for cpuStructure for cpu
Structure for cpu
 

Más de yndaravind

Más de yndaravind (13)

Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects
 
The Object Model
The Object Model  The Object Model
The Object Model
 
OOAD
OOADOOAD
OOAD
 
OOAD
OOADOOAD
OOAD
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Repetations in C
Repetations in CRepetations in C
Repetations in C
 
Selection & Making Decisions in c
Selection & Making Decisions in cSelection & Making Decisions in c
Selection & Making Decisions in c
 
Bitwise Operators in C
Bitwise Operators in CBitwise Operators in C
Bitwise Operators in C
 
Strings part2
Strings part2Strings part2
Strings part2
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Functions part1
Functions part1Functions part1
Functions part1
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 

Structure In C

  • 1. @2020 Presented By Y. N. D. Aravind 1 Presented By Y. N. D. ARAVIND M.Tech, Dept of CSE Newton’s Group Of Institutions, Macherla
  • 2. Session Objectives Unions Typedef Enumerated Data Type @2020 Presented By Y. N. D. Aravind 2 2 Structures
  • 3. A Structure can be defined to be a group of logically related data items, which may be of different types, stored in contiguous memory locations, sharing a common name, but distinguished by its members. The decleration of a structure specifies the grouping of various data items into a single unit without assigning any resources to them. The syntax for declaring a structure in c is as follows struct TAG { Data Type varaible-1; Data Type variable-2; …… …… Data Type variable-n; }; Where,  struct is the keyword which tells the compiler that a structure is being defined.  Tag_name is the name of the structure.  variable1, variable2 … are called members of the structure.  The members are declared within curly braces.  The closing brace must end with the semicolon. @2020 Presented By Y. N. D. Aravind 3 3 Example : struct employee { int eno; char name[80]; float sal; };
  • 4. Structure Definition The decleration of a structure will not serve any purpose without its definition. It only acts as a bluprint for the creation of variables of type struct. The structure definition creates structure variable and allocates storage space for them. Method 1 : struct Structurename var1, var2, . . . . . ; Example : struct employee x; Example : struct employee x, y, z ; Method 2 : @2020 Presented By Y. N. D. Aravind 4 4 Example : struct employee { int eno; char name[80]; float sal; }; 2 bytes 80 bytes 4 bytes Example : struct employee { int eno; char name[80]; float sal; }x; The structure variable can be created during the decleration as follows
  • 5. Accessing Structure Members @2020 Presented By Y. N. D. Aravind 5 The members of a structure can be accessed by using dot(.) operator. dot (.) operator Structures use a dot (.) operator(also called period operator or member operator) to refer its elements. Before dot, there must always be a structure variable. After the dot, there must always be a structure element. syntax :- structure_variable_name . structure_member_name struct student { char name [5]; int roll_number; float avg; }; struct student s1; The members can be accessed using the variables as shown below, s1.name --> refers the string name s1.roll_number --> refers the roll_number s1.avg --> refers avg 5
  • 6. Structure Initilization @2020 Presented By Y. N. D. Aravind 6 Like any other data types, structure variables can be initialized at the point of their definition. Consider the following structure decleration. struct student { char name [5]; int roll_number; float avg; }; Example :- A variable of the structure employee can be initialized during its defination as folloes: struct student s1 = {“Ravi”, 10, 67.8}; 6 name roll no avg
  • 7. Array of Structures @2020 Presented By Y. N. D. Aravind 7 An array is a collection of elements of same data type that are stored in contiguous memory locations. A structure is a collection of members of different data types stored in contiguous memory locations. An array of structures is an array in which each element is a structure. This concept is very helpful in representing multiple records of a file, where each record is a collection of dissimilar data items. As we have an array of integers, we can have an array of structures also. For example, suppose we want to store the information of class of students, consisting of roll_number, name and percentage, A better approach would be to use an array of structures. Array of structures can be declared as follows, struct tag_name arrayofstructure[size]; Let‟s take an example, to store the information of 20 students, we can have the following structure definition and declaration, struct student { int rollno; char name[80]; float per; }; struct student s[20]; 7
  • 8. Program to illustrate an array of structures @2020 Presented By Y. N. D. Aravind 8 #include<stdio.h> #include<conio.h> struct student { int rollno; char name[80]; float per; }; void main( ) { struct student s[20]; int i,n; clrscr(); printf("Enter no of students [ 1 – 20 ] n "); scanf(“%d”,&n); printf("Enter %d students details n "); for(i = 0; i < n; i++) scanf(“%d %s %f”,&s[i].rollno,s[i].name,&s[i].per); printf("n Student details: n"); for(i=0; i<n; i++) printf(“%3d %20s %9.2f n”, s[i].rollno,s[i].name,s[i].per); getch((); } 8 Input-Output Enter no of students [ 1 – 20 ] 2 Enter 2 students details 1 Ramesh 90 2 Anusha 80 Student details: 1 Ramesh 90.00 2 Anusha 80 .00
  • 9. Array Within Structures @2020 Presented By Y. N. D. Aravind 9 It is also possible to declare an array as a member of structure, like declaring ordinary variables. For example to store marks of a student in six subjects then we can have the following definition of a structure. Example :- struct student { int rollno; char name[80]; int marks[6]; }; Then the initialization of the array marks done as follows, struct student s1= {“ravi”, 34, {60,70,80,68,90,75}}; The values of the member marks array are referred as follows, s1.marks [0] --> will refer the 0th element in the marks 60 s1.marks [1] --> will refer the 1st element in the marks 70 s1.marks [2] --> will refer the 2nd element in the marks 80 s1.marks [3] --> will refer the 3rd element in the marks 68 s1.marks [4] --> will refer the 4th element in the marks 90 s1.marks [5] --> will refer the 5th element in the marks 75 9
  • 10. To create a list of students details and display them. @2020 Presented By Y. N. D. Aravind 10 #include<stdio.h> #include<conio.h> struct student { int rollno; char name[80]; int marks[6]; int sum; float per; }; void main( ) { struct student x[20]; int i,n,j; clrscr(); printf("Enter no of students [ 1 – 20 ] n "); scanf(“%d”,&n); for(i = 0; i < n; i++) { printf("Enter rollno , name of students - %d n “,i+1); scanf(“%d %s ”,&x[i].rollno,x[i].name); Printf(“ Enter marks in six subjects n”): for(j = 0; j < 6; j++) scanf(“%d”, &x[i].marks[j]); } for(i = 0; i < n; i++) { x[i].sum = 0; for(j = 0; j < 6; j++) x[i].sum += x[i].marks[j]; x[i].per = (float) x[i].sum / 6; } printf(“ Roll no t Name t Percentage n”); for(i=0; i < n; i++) printf(“%3f %20s %9.2f n”,x[i].rollno,x[i].name, x[i].per); getch(); } Input – Output Enter no of students [ 1 – 20 ] 2 Enter rollno , name of students –1 1 Ramesh Enter marks in six subjects 68 79 77 60 91 75 Enter rollno , name of students – 2 2 Anusha Enter marks in six subjects 88 68 66 60 90 74 Roll no Name Percentage 1 Ramesh 75.00 2 Anusha 74.40
  • 11. Structure Within Structure @2020 Presented By Y. N. D. Aravind 11 A structure which includes another structure is called nested structure or structure within structure. i.e a structure can be used as a member of another structure. There are two methods for declaration of nested structures. 11 1. The syntax for the nesting of the structure is as follows struct tag_name1 { type1 member1; ……. ……. }; struct tag_name2 { type1 member1; …… …… struct tag_name1 var; …… }; The syntax for accessing members of a nested structure as follows, outer_structure_variable . inner_structure_variable . member_name 2. The syntax of another method for the nesting of the structure is as follows struct structure_nm { <data-type> element 1; <data-type> element 2; - - - - - - - - - - - - - - - - - - - - - - <data-type> element n; struct structure_nm { <data-type> element 1; <data-type> element 2; - - - - - - - - - - - - - - - - - - - - - - <data-type> element n; }inner_struct_var; }outer_struct_var;
  • 12. To illustrate the concept of structure within structure. @2020 Presented By Y. N. D. Aravind 12 #include<stdio.h> #include<conio.h> Struct date { int day; int month; int year; }; struct student { int rollno; char name[80]; float per; struct date doa; }; void main( ) { struct student x; clrscr(); printf("Enter Roll no Name DOB and Per n "); scanf(“%d %s %d %d %d %f”,&x.rollno, x.name,&x.doa.day, &x.doa.month, &x.doa.year,&x.per); printf(“ Roll no : %d n”,x.rollno); printf(“ Name : %s n”,x.name); printf(“ Percentage =:%3.2f n”,x.per); printf(“ DOB : %d / %d /%d n”, x.doa.day,x.doa.month,x.doa.year); getch(); } Input – Output Enter Rollno Name DOB and Per 1 Ramesh 20 10 2008 90 Roll no : 1 Name : Ramesh Percentage : 90.00 DOB : 20/10/2008
  • 13. Self Referential Structure @2020 Presented By Y. N. D. Aravind 13 A structure contaning a pointer to the same type is referred to as self- referential structure. It is useful for implementing data structures such as linked list, queues, stacks and trees etc.. A linked list consists of structures related to each other through pointers. The self referential pointer in the structure points to the next node of a list. 13 Syntax:- struct tag_name { Type1 member1; Type2 member2; ………. struct tag_name *next; }; Ex:-struct node { int data; node struct node *next; } n1, n2; data next
  • 14. UNION @2020 Presented By Y. N. D. Aravind 14 A union is one of the derived data types. Union is a collection of variables referred under a single name. The syntax, declaration and use of union is similar to the structure but its functionality is different 14 Syntax:- Union Union_name { Type1 member1; Type2 member2; ………. ............ Typen membern; } Union is a keyword , Union name is any user defined name which should be a valid C identifier.Data type is any valid data type supported by C or user-defined type. Member-1, member-2, ....,member-n are the members of union. The syntax of declaring a variable of union type is Syntax:- Union Union_name var1,var2,.......................
  • 15. ENUMERATED DATA TYPE @2020 Presented By Y. N. D. Aravind 15 Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. The syntax of defining an enumerated type is as follows: The syntax of declaring a variable of enum type is similar to that used while declaring variables of structure or union type Example :- enum boolean{true, false}; enum boolean t,f; t= true; f= false; The compiler automatically assigns integer digits begining with 0 to all the enumerated constants. „i.e‟ the enumerated constant true is assigned 0, false is assigned 1 15 Syntax:- enum identifier{ value-1,varlue-2,......................value-n}; Syntax:- enum identifier v1,v2,......................vn;
  • 16. ENUMERATED DATA TYPE @2020 Presented By Y. N. D. Aravind 16 However, the automatically assignments can be overridden by assigning values explicitly to the enumerated constants. enum State {Working = 1, Failed}; Here, the constant working is assigned the value of 1, the remaning constants are assigned values that increase successively by 1. 16 #include<stdio.h> #include<conio.h> void main( ) { enum month{ Jan, Feb, Mar, Apr, May, June, July, Aug, Sep, Oct, Nov, Dec}; enum month year_st, year_end; clrscr(); year_st = Jan; year_end = Dec; printf(“Jan = %d n”, year_st); printf(“ Feb = %d n “, year_end; getch(); } Input –Output Jan = 0 Dec =11
  • 17. Typedef @2020 Presented By Y. N. D. Aravind 17 typedef is a keyword used in C language to assign alternative names to existing datatypes. Its mostly used with user defined datatypes, when names of the datatypes become slightly complicated to use in programs. Following is the general syntax for using typedef, typedef <existing_name> <alias_name>; Lets take an example and see how typedef actually works. typedef unsigned long ulong; The above statement define a term ulong for an unsigned long datatype. Now this ulong identifier can be used to define unsigned long type variables. ulong i, typedef can be used to give a name to user defined data type as well. Lets see its use with structures. typedef struct { type member1; type member2; type member3; } type_name; Here type_name represents the stucture definition associated with it. Now this type_name can be used to declare a variable of this stucture type. type_name t1, t2; 17
  • 18. Thank You @2020 Presented By Y. N. D. Aravind 18 Presented By Y. N. D. ARAVIND M.Tech, Dept of CSE Newton’s Group Of Institutions, Macherla