SlideShare a Scribd company logo
PRESEG
K
1
2
Submitted By:
Gurvinder Kaur
L-2019-A-22-BTFT
COMPUTER PROGRAMMING AND
DATA STRUCTURES
Submitted To:
Mr. Ashok Kumar
STRUCTURES
3
In C programming, a struct
(or structure) is a collection
of variables (can be of
different types) under a
single name.
4
5
How to define
structures?
To define a struct,
the struct keyword
is used.
Structure always ends
with a semicolon(;)
struct employees
{
char name[20];
int age;
float salary;
};
Here is an example:
struct structure_Name
{
dataType member1;
dataType member2;
... };
Syntax of struct
Here, a derived type structure employees is defined.
6
Create structure
variables
When a struct type is
declared, no storage or
memory is allocated. To
allocate memory of a given
structure type and work
with it, we create variables
struct employees
{
char name[20];
int age;
float salary;
};
int main()
{
struct employees employee1, employee2, e[20];
return 0;
}Two variables employee1,
employee2, and an array variable e
having 20 elements of type struct
employees are created.
Alternative method
struct tag
{
member 1;
member 2;
....
member m;
} var_1, var_2,...var_n;
Declares three
variables of type
struct tag
7
Access members
of a structure
There are two types of
operators used for accessing
members of a structure.
. - Member operator
-> - Structure pointer operator
EXAMPLE:-
To access the salary of employee2
employee2.salary
 In order to assign a value to any
structure member, the member name
must be linked with
the structure variable using a
dot . operator also
called period or member
access operator.
#include <stdio.h>
#include <conio.h>
struct employees
{
char name[20];
int age;
float salary;
};
int main( )
{
/* Declare employee1 of type employees */
struct employees employee1 ={“Sofia”,27, 35000};
/* Declare employee2 of type employees */
struct employees employee2={“Blossom”,30, 30000};
/* print employee1 info */
printf( “employee1 name: %sn", employee1.name);
printf( “employee1 age: %dn", employee1.age);
printf( “employee1 salary: %fn", employee1.salary);
/* print employee2 info */
printf( “employee2 name: %sn", employee2.name);
printf( " employee2 age : %dn", employee2.age);
printf( “employee2 salary: %fn", employee2.salary);
return 0;
}
PROGRAM TO ACCESS AND PRINT MEMBERS OF A STRUCTURE
employee1 name: Sofia
employee1 age: 27
employee1 salary: 35000.0
employee2 name: Blossom
employee2 age: 30
employee2 salary: 30000.0
OUTPUT
8
9
Structures as
Function
Arguments
We can pass a structure
as a function argument
in the same way as we
pass any other variable
or pointer.
struct employees
{
char name[20];
int age;
float salary;
};
int manager()
{
struct employees manager;
………
}
int main()
{
struct employees employee1;
struct employees employee2;
………..
}
Manager information is
maintained in a separate
function.
So function manager() is
declared. We have declared a
variable manager in it which is
local to the function
manager()
10
#include<stdio.h>
#include<conio.h>
struct employees
{
char name;
int age;
int salary;
};
int manager()
{
struct employees manager;
manager.age=28;
if(manager.age>30)
manager.salary=65000;
else
manager.salary=55000;
return manager.salary;
}
int main()
{ struct employees employee1;
struct employees employee2;
printf(“Enter the salary of employee1:”);
scanf(“%d”, &employee1.salary);
printf(“Enter the salary of employee2:”);
scanf(“%d”, &employee2.salary);
printf(“ Employee 1 salary is: %dn”,employee1.salary);
printf(“ Employee 2 salary is: %dn”,employee2.salary);
printf(“ Manager’s salary is: %dn”,manager());
return 0;
}
OUTPUT
Enter the salary of employee1: 35000
Enter the salary of employee2: 20000
Employee 1 salary is: 35000
Employee 2 salary is: 20000
Manager’s salary is: 55000
PROGRAM SHOWING STRUCTURE AS FUNCTION ARGUMENTS
Array of Structures in
C
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.
Once a structure has been
defined, we can declare an
array of structures
for example;
struct student class[50];
The individual members
can be accessed as
class[i].name
class[5].roll_number
Example of an array of structures that stores information of 3
students and prints it.
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main()
{
int i;
struct student st[3];
printf("Enter Records of 3 students");
for(i=0;i<3;i++)
{
printf("nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("nEnter Name:");
scanf("%s",&st[i].name);
}
printf("nStudent Information List:");
for(i=0;i<3;i++){
printf("nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
OUTPUT
Enter Records of 3 students
Enter Rollno: 1
Enter Name: Jamie
Enter Rollno: 2
Enter Name: Ratan
Enter Rollno: 3
Enter Name: John
Student Information List:
Rollno:1, Name: Jamie
Rollno:2, Name: Ratan
Rollno:3, Name: John
Pointers to
Structures
13
struct abc
{
int x;
int y;
};
int main()
{
struct abc a={0,1};
struct abc *ptr=&a;
printf(“%d %d”, ptr->x, ptr->y);
return 0;
}
ptr is a pointer to
some variable of
type struct abc
We can define pointers to structures in
the same way as we define pointer to
any other variable
We can store the address of a structure
variable (a) in the defined pointer
variable(*ptr)
To find the address of a structure
variable, place the '&'; operator before
the structure's name
To access the members of a structure
using a pointer to that structure, we use
the → operator ;
0,1
OUTPUT
14
Nested
Structure in C
C provides us the feature of
nesting one structure within
another structure by using
which, complex data types are
created.
Nested structures means, that one
structure has another stucture as
member variable.
struct student
{
char[30] name;
int age;
/* here Address is a structure */
struct Address
{
char[50] locality;
char[50] city;
int pincode;
} addr;
WATCHING
THANKS
FOR
Ad

More Related Content

What's hot (20)

Structure & union
Structure & unionStructure & union
Structure & union
Rupesh Mishra
 
C語言基本資料型別與變數
C語言基本資料型別與變數C語言基本資料型別與變數
C語言基本資料型別與變數
吳錫修 (ShyiShiou Wu)
 
Function basics
Function basicsFunction basics
Function basics
mohamed sikander
 
Structure and union
Structure and unionStructure and union
Structure and union
Samsil Arefin
 
Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
Vijayananda Ratnam Ch
 
Roadmap to Object Oriented Programming
Roadmap to Object Oriented ProgrammingRoadmap to Object Oriented Programming
Roadmap to Object Oriented Programming
ssuser6fc8b1
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
baabtra.com - No. 1 supplier of quality freshers
 
C語言函式
C語言函式C語言函式
C語言函式
吳錫修 (ShyiShiou Wu)
 
L10
L10L10
L10
lksoo
 
Listing for TryLambdaExpressions
Listing for TryLambdaExpressionsListing for TryLambdaExpressions
Listing for TryLambdaExpressions
Derek Dhammaloka
 
201801 CSE240 Lecture 09
201801 CSE240 Lecture 09201801 CSE240 Lecture 09
201801 CSE240 Lecture 09
Javier Gonzalez-Sanchez
 
Structure & union
Structure & unionStructure & union
Structure & union
lalithambiga kamaraj
 
Presentation for structure in c
Presentation for  structure in cPresentation for  structure in c
Presentation for structure in c
MdSaydurRahmanRifat
 
Structure & Union in C++
Structure & Union in C++Structure & Union in C++
Structure & Union in C++
Davinder Kaur
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
Mook Prapasson
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
YOGESH SINGH
 
OO-like C Programming: Struct Inheritance and Virtual Function
OO-like C Programming: Struct Inheritance and Virtual FunctionOO-like C Programming: Struct Inheritance and Virtual Function
OO-like C Programming: Struct Inheritance and Virtual Function
Yu-Sheng (Yosen) Chen
 
Lab 13
Lab 13Lab 13
Lab 13
Adnan Raza
 
week-20x
week-20xweek-20x
week-20x
KITE www.kitecolleges.com
 
Structures
StructuresStructures
Structures
Vernie Gamit
 

Similar to STRUCTURES IN C PROGRAMMING (20)

detail structure presentation of problem solving
detail structure presentation of problem solvingdetail structure presentation of problem solving
detail structure presentation of problem solving
talencorconsultancy
 
Programming for problem solving-II(UNIT-2).pptx
Programming for problem solving-II(UNIT-2).pptxProgramming for problem solving-II(UNIT-2).pptx
Programming for problem solving-II(UNIT-2).pptx
prathima304
 
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
sudhakargeruganti
 
03 structures
03 structures03 structures
03 structures
Rajan Gautam
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
nikshaikh786
 
Principals of Programming in CModule -5.pdf
Principals of Programming in CModule -5.pdfPrincipals of Programming in CModule -5.pdf
Principals of Programming in CModule -5.pdf
anilcsbs
 
structure.ppt
structure.pptstructure.ppt
structure.ppt
Sheik Mohideen
 
C structure and union
C structure and unionC structure and union
C structure and union
Thesis Scientist Private Limited
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Smit Shah
 
C Language_PPS_3110003_unit 8ClassPPT.ppt
C Language_PPS_3110003_unit 8ClassPPT.pptC Language_PPS_3110003_unit 8ClassPPT.ppt
C Language_PPS_3110003_unit 8ClassPPT.ppt
NikeshaPatel1
 
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
PPS 8.8.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)PPS 8.8.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
Sitamarhi Institute of Technology
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
Alisha Korpal
 
structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
AbhimanyuKumarYadav3
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
Krishna Nanda
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structure
Deepak Singh
 
Chapter 2 part II array and structure.pptx
Chapter 2 part II array and structure.pptxChapter 2 part II array and structure.pptx
Chapter 2 part II array and structure.pptx
abenezertekalign118
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
Gem WeBlog
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
arnold 7490
 
Structures
StructuresStructures
Structures
archikabhatia
 
detail structure presentation of problem solving
detail structure presentation of problem solvingdetail structure presentation of problem solving
detail structure presentation of problem solving
talencorconsultancy
 
Programming for problem solving-II(UNIT-2).pptx
Programming for problem solving-II(UNIT-2).pptxProgramming for problem solving-II(UNIT-2).pptx
Programming for problem solving-II(UNIT-2).pptx
prathima304
 
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
sudhakargeruganti
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
nikshaikh786
 
Principals of Programming in CModule -5.pdf
Principals of Programming in CModule -5.pdfPrincipals of Programming in CModule -5.pdf
Principals of Programming in CModule -5.pdf
anilcsbs
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Smit Shah
 
C Language_PPS_3110003_unit 8ClassPPT.ppt
C Language_PPS_3110003_unit 8ClassPPT.pptC Language_PPS_3110003_unit 8ClassPPT.ppt
C Language_PPS_3110003_unit 8ClassPPT.ppt
NikeshaPatel1
 
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
PPS 8.8.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)PPS 8.8.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
Sitamarhi Institute of Technology
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
Alisha Korpal
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
Krishna Nanda
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structure
Deepak Singh
 
Chapter 2 part II array and structure.pptx
Chapter 2 part II array and structure.pptxChapter 2 part II array and structure.pptx
Chapter 2 part II array and structure.pptx
abenezertekalign118
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
Gem WeBlog
 
Ad

More from Gurwinderkaur45 (7)

ANCIENT METHODS OF FOOD PRESERVATION
ANCIENT METHODS OF FOOD PRESERVATIONANCIENT METHODS OF FOOD PRESERVATION
ANCIENT METHODS OF FOOD PRESERVATION
Gurwinderkaur45
 
ANTIOXIDANTS POWERPOINT PRESENTATION
ANTIOXIDANTS POWERPOINT  PRESENTATIONANTIOXIDANTS POWERPOINT  PRESENTATION
ANTIOXIDANTS POWERPOINT PRESENTATION
Gurwinderkaur45
 
Animatronics POWERPOINT PRESENTATION
Animatronics POWERPOINT PRESENTATIONAnimatronics POWERPOINT PRESENTATION
Animatronics POWERPOINT PRESENTATION
Gurwinderkaur45
 
Lifi powerpoint presentation
Lifi powerpoint presentationLifi powerpoint presentation
Lifi powerpoint presentation
Gurwinderkaur45
 
plant hormones powerpoint presentation
plant hormones powerpoint presentationplant hormones powerpoint presentation
plant hormones powerpoint presentation
Gurwinderkaur45
 
IMMUNITY AND THE IMMUNE SYSTEM
IMMUNITY AND THE IMMUNE SYSTEMIMMUNITY AND THE IMMUNE SYSTEM
IMMUNITY AND THE IMMUNE SYSTEM
Gurwinderkaur45
 
Pros and cons of Science and technology
Pros and cons of Science and technologyPros and cons of Science and technology
Pros and cons of Science and technology
Gurwinderkaur45
 
ANCIENT METHODS OF FOOD PRESERVATION
ANCIENT METHODS OF FOOD PRESERVATIONANCIENT METHODS OF FOOD PRESERVATION
ANCIENT METHODS OF FOOD PRESERVATION
Gurwinderkaur45
 
ANTIOXIDANTS POWERPOINT PRESENTATION
ANTIOXIDANTS POWERPOINT  PRESENTATIONANTIOXIDANTS POWERPOINT  PRESENTATION
ANTIOXIDANTS POWERPOINT PRESENTATION
Gurwinderkaur45
 
Animatronics POWERPOINT PRESENTATION
Animatronics POWERPOINT PRESENTATIONAnimatronics POWERPOINT PRESENTATION
Animatronics POWERPOINT PRESENTATION
Gurwinderkaur45
 
Lifi powerpoint presentation
Lifi powerpoint presentationLifi powerpoint presentation
Lifi powerpoint presentation
Gurwinderkaur45
 
plant hormones powerpoint presentation
plant hormones powerpoint presentationplant hormones powerpoint presentation
plant hormones powerpoint presentation
Gurwinderkaur45
 
IMMUNITY AND THE IMMUNE SYSTEM
IMMUNITY AND THE IMMUNE SYSTEMIMMUNITY AND THE IMMUNE SYSTEM
IMMUNITY AND THE IMMUNE SYSTEM
Gurwinderkaur45
 
Pros and cons of Science and technology
Pros and cons of Science and technologyPros and cons of Science and technology
Pros and cons of Science and technology
Gurwinderkaur45
 
Ad

Recently uploaded (20)

Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 

STRUCTURES IN C PROGRAMMING

  • 2. 2 Submitted By: Gurvinder Kaur L-2019-A-22-BTFT COMPUTER PROGRAMMING AND DATA STRUCTURES Submitted To: Mr. Ashok Kumar
  • 4. In C programming, a struct (or structure) is a collection of variables (can be of different types) under a single name. 4
  • 5. 5 How to define structures? To define a struct, the struct keyword is used. Structure always ends with a semicolon(;) struct employees { char name[20]; int age; float salary; }; Here is an example: struct structure_Name { dataType member1; dataType member2; ... }; Syntax of struct Here, a derived type structure employees is defined.
  • 6. 6 Create structure variables When a struct type is declared, no storage or memory is allocated. To allocate memory of a given structure type and work with it, we create variables struct employees { char name[20]; int age; float salary; }; int main() { struct employees employee1, employee2, e[20]; return 0; }Two variables employee1, employee2, and an array variable e having 20 elements of type struct employees are created. Alternative method struct tag { member 1; member 2; .... member m; } var_1, var_2,...var_n; Declares three variables of type struct tag
  • 7. 7 Access members of a structure There are two types of operators used for accessing members of a structure. . - Member operator -> - Structure pointer operator EXAMPLE:- To access the salary of employee2 employee2.salary  In order to assign a value to any structure member, the member name must be linked with the structure variable using a dot . operator also called period or member access operator.
  • 8. #include <stdio.h> #include <conio.h> struct employees { char name[20]; int age; float salary; }; int main( ) { /* Declare employee1 of type employees */ struct employees employee1 ={“Sofia”,27, 35000}; /* Declare employee2 of type employees */ struct employees employee2={“Blossom”,30, 30000}; /* print employee1 info */ printf( “employee1 name: %sn", employee1.name); printf( “employee1 age: %dn", employee1.age); printf( “employee1 salary: %fn", employee1.salary); /* print employee2 info */ printf( “employee2 name: %sn", employee2.name); printf( " employee2 age : %dn", employee2.age); printf( “employee2 salary: %fn", employee2.salary); return 0; } PROGRAM TO ACCESS AND PRINT MEMBERS OF A STRUCTURE employee1 name: Sofia employee1 age: 27 employee1 salary: 35000.0 employee2 name: Blossom employee2 age: 30 employee2 salary: 30000.0 OUTPUT 8
  • 9. 9 Structures as Function Arguments We can pass a structure as a function argument in the same way as we pass any other variable or pointer. struct employees { char name[20]; int age; float salary; }; int manager() { struct employees manager; ……… } int main() { struct employees employee1; struct employees employee2; ……….. } Manager information is maintained in a separate function. So function manager() is declared. We have declared a variable manager in it which is local to the function manager()
  • 10. 10 #include<stdio.h> #include<conio.h> struct employees { char name; int age; int salary; }; int manager() { struct employees manager; manager.age=28; if(manager.age>30) manager.salary=65000; else manager.salary=55000; return manager.salary; } int main() { struct employees employee1; struct employees employee2; printf(“Enter the salary of employee1:”); scanf(“%d”, &employee1.salary); printf(“Enter the salary of employee2:”); scanf(“%d”, &employee2.salary); printf(“ Employee 1 salary is: %dn”,employee1.salary); printf(“ Employee 2 salary is: %dn”,employee2.salary); printf(“ Manager’s salary is: %dn”,manager()); return 0; } OUTPUT Enter the salary of employee1: 35000 Enter the salary of employee2: 20000 Employee 1 salary is: 35000 Employee 2 salary is: 20000 Manager’s salary is: 55000 PROGRAM SHOWING STRUCTURE AS FUNCTION ARGUMENTS
  • 11. Array of Structures in C 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. Once a structure has been defined, we can declare an array of structures for example; struct student class[50]; The individual members can be accessed as class[i].name class[5].roll_number
  • 12. Example of an array of structures that stores information of 3 students and prints it. #include<stdio.h> #include <string.h> struct student{ int rollno; char name[10]; }; int main() { int i; struct student st[3]; printf("Enter Records of 3 students"); for(i=0;i<3;i++) { printf("nEnter Rollno:"); scanf("%d",&st[i].rollno); printf("nEnter Name:"); scanf("%s",&st[i].name); } printf("nStudent Information List:"); for(i=0;i<3;i++){ printf("nRollno:%d, Name:%s",st[i].rollno,st[i].name); } return 0; } OUTPUT Enter Records of 3 students Enter Rollno: 1 Enter Name: Jamie Enter Rollno: 2 Enter Name: Ratan Enter Rollno: 3 Enter Name: John Student Information List: Rollno:1, Name: Jamie Rollno:2, Name: Ratan Rollno:3, Name: John
  • 13. Pointers to Structures 13 struct abc { int x; int y; }; int main() { struct abc a={0,1}; struct abc *ptr=&a; printf(“%d %d”, ptr->x, ptr->y); return 0; } ptr is a pointer to some variable of type struct abc We can define pointers to structures in the same way as we define pointer to any other variable We can store the address of a structure variable (a) in the defined pointer variable(*ptr) To find the address of a structure variable, place the '&'; operator before the structure's name To access the members of a structure using a pointer to that structure, we use the → operator ; 0,1 OUTPUT
  • 14. 14 Nested Structure in C C provides us the feature of nesting one structure within another structure by using which, complex data types are created. Nested structures means, that one structure has another stucture as member variable. struct student { char[30] name; int age; /* here Address is a structure */ struct Address { char[50] locality; char[50] city; int pincode; } addr;