SlideShare une entreprise Scribd logo
1  sur  30
STRUCTURESTRUCTURE in CC
programming…
Group : ASTUTE
Soummo Supriya : 151-15-4741
T.M. Ashikur Rahman : 151-15-4971
Md. Fazle Rabbi Ador : 151-15-5482
Tasnuva Tabassum Oshin : 151-15-4673
Masumer Rahman : 151-15-5040
Introduction
• Md. Fazle Rabbi adoR
• 151-15-5482
Structure
• A structure is a collection of variables of
different data types under a single name.
• The variables are called members of the
structure.
• The structure is also called a user-defined
data type.
3
Defining a Structure
• Syntax:
struct structure_name
{
data_type member_variable1;
data_type member_variable2;
………………………………;
data_type member_variableN;
};
Note: The members of a structure do not occupy
memory until they are associated with a
structure_variable.
4
struct student
{
char name[20];
int roll_no;
float marks;
char gender;
long int phone_no;
};
•Multiple variables of struct student type can be
declared as:
struct student st1, st2, st3; 5
Example
Defining a structure…Defining a structure…
• Each variable of structure has its own
copy of member variables.
• The member variables are accessed using
the dot (.) operator or member operator.
• For example: st1.name is member
variable name of st1 structure variable
while st3.gender is member variable
gender of st3 structure variable.
6
struct student
{
char name[20];
int ID;
float CSE_marks;
char gender;
long int phone_no;
};
void main()
{
struct student st1={"Ishtiaque",5482,13.5,'M',16021548};
struct student st2={"Oshin",4288,15,'F',19845321};
printf ("Name: %s ID: %d CSE Marks: %.1f Gender: %c Phn: %d
n",st1.name,st1.ID,st1.CSE_marks,st1.gender,st1.phone_no);
printf ("Name: %s ID: %d CSE Marks: %.1f Gender: %c Phn: %d
n",st2.name,st2.ID,st2.CSE_marks,st2.gender,st2.phone_no);
}
7
Pointers to structurePointers to structure
A pointer is a memory address.
You can define pointers to
structures in very similar way as
you define pointer to any other
variable
Prepared By Soummo Spriya
151-15-4741
Declaration of pointers
• Referencing pointer to
another address to access
memory
• Using dynamic memory
allocation
Structure's member through
pointer can be used in two ways:
““Structure within anotherStructure within another
StructureStructure
(Nested Structure)”(Nested Structure)”
Md.Masumer Rahman
Id: 151-15-5040
What is Nested Structure
• Nested structure in C is nothing but structure within
structure. One structure can be declared inside other
structure as we declare structure members inside a
structure. The structure variables can be a normal
structure variable or a pointer variable to access the
data.
• Nested Structures are allowed in C Programming
Language.
• We can write one Structure inside another structure
as member of another structure.
Let’s see the structure declaration below,
Way 1: Declare two separate structures
struct date
{
int date;
int month;
int year;
};
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date doj;
}
emp1;
Way 2 : Declare Embedded structures
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date
{
int date;
int month;
int year;
}
doj;
}
emp1;
Function and Structure
Name: T.m.
ashikur rahmaN
id: 151-15-4971
Function and Structure
We will consider four cases
here:
Passing the individual member to
functions
 Passing whole structure to functions
Passing structure pointer to functions
Passing array of structure to functions
Passing the individual member to
functions
 Structure members can be
passed to functions as actual
arguments in function call
like ordinary variables.
PASSING WHOLE STRUCTURE TO
FUNCTIONS
 Whole structure can be passed to a function
by the syntax:
function _ name ( structure _ variable _
name );
 The called function has the form:
return _ type function _ name (struct tag _ name
structure _ variable _ name)
{
…………;
}
Passing structure pointer to
functions
 In this case, address of structure
variable is passed as an actual argument
to a function.
The corresponding formal argument must
be a structure type pointer variable.
Passing array of structure to function
 Passing an array of structure type to a
function is similar to passing an array of
any type to a function.
 That is, the name of the array of
structure is passed by the calling function
which is the base address of the array of
structure.
 Note: The function prototype comes after
the structure definition.
Arrays of structuresArrays of structures
• Tasnuva Tabassum Oshin
• 151-15-4673
Arrays of structures
• An ordinary array: One type of data
• An array of structs: Multiple types of data
in each array element.
0 1 2 … 98 99
0 1 2 … 98 99
Arrays of structures
• We often use arrays of structures.
• Example:
StudentRecord Class[100];
strcpy(Class[98].Name, “bangladeshi man");
Class[98].Id = 12345;
strcpy(Class[98].Dept, "COMP");
Class[98].gender = 'M';
Class[0] = Class[98]; Bangladeshi man
12345 M
COMP
. . .
0 1 2 … 98 99
Arrays inside structures
• We can use arrays inside structures.
• Example:
struct square{
point vertex[4];
};
square Sq;
• Assign values to Sq using the given square
x y x y x y x y
(4, 3) (10, 3)
(4, 1) (10, 1)
Structure in C
Structure in C

Contenu connexe

Tendances (20)

Structure in c
Structure in cStructure in c
Structure in c
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
 
Strings in c
Strings in cStrings in c
Strings in c
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 
Enums in c
Enums in cEnums in c
Enums in c
 
structure and union
structure and unionstructure and union
structure and union
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
C pointer
C pointerC pointer
C pointer
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
C Pointers
C PointersC Pointers
C Pointers
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Structure & union
Structure & unionStructure & union
Structure & union
 

En vedette (8)

Structure c
Structure cStructure c
Structure c
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
String functions in C
String functions in CString functions in C
String functions in C
 
Strings in C
Strings in CStrings in C
Strings in C
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Array in c language
Array in c languageArray in c language
Array in c language
 
String c
String cString c
String c
 
String in c
String in cString in c
String in c
 

Similaire à Structure in C

Similaire à Structure in C (20)

User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
Structures
StructuresStructures
Structures
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
 
1. structure
1. structure1. structure
1. structure
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
 
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
 
Structure
StructureStructure
Structure
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Pointers and Structures.pptx
Pointers and Structures.pptxPointers and Structures.pptx
Pointers and Structures.pptx
 
Structure In C
Structure In CStructure In C
Structure In C
 
Structures_Final_KLE (2).pptx
Structures_Final_KLE (2).pptxStructures_Final_KLE (2).pptx
Structures_Final_KLE (2).pptx
 
Struct
StructStruct
Struct
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 
CHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptxCHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptx
 
Structures in C
Structures in CStructures in C
Structures in C
 
C structure and union
C structure and unionC structure and union
C structure and union
 
Structures
StructuresStructures
Structures
 

Plus de Fazle Rabbi Ador

Online Shopping Agent in AI
Online Shopping Agent in AIOnline Shopping Agent in AI
Online Shopping Agent in AIFazle Rabbi Ador
 
00 java basic programming in Bangla|| Introduction
00 java basic programming in Bangla|| Introduction00 java basic programming in Bangla|| Introduction
00 java basic programming in Bangla|| IntroductionFazle Rabbi Ador
 
Types of memory in Computer
Types of memory in ComputerTypes of memory in Computer
Types of memory in ComputerFazle Rabbi Ador
 
Error Finding in Numerical method
Error Finding in Numerical methodError Finding in Numerical method
Error Finding in Numerical methodFazle Rabbi Ador
 
Data Mining & Applications
Data Mining & ApplicationsData Mining & Applications
Data Mining & ApplicationsFazle Rabbi Ador
 
Electromagnetic induction & useful applications
Electromagnetic induction & useful applicationsElectromagnetic induction & useful applications
Electromagnetic induction & useful applicationsFazle Rabbi Ador
 
Application of mathematics in daily life
Application of mathematics in daily lifeApplication of mathematics in daily life
Application of mathematics in daily lifeFazle Rabbi Ador
 
Application of mathematics in daily life
Application of mathematics in daily lifeApplication of mathematics in daily life
Application of mathematics in daily lifeFazle Rabbi Ador
 

Plus de Fazle Rabbi Ador (12)

Online Shopping Agent in AI
Online Shopping Agent in AIOnline Shopping Agent in AI
Online Shopping Agent in AI
 
00 java basic programming in Bangla|| Introduction
00 java basic programming in Bangla|| Introduction00 java basic programming in Bangla|| Introduction
00 java basic programming in Bangla|| Introduction
 
Types of memory in Computer
Types of memory in ComputerTypes of memory in Computer
Types of memory in Computer
 
Error Finding in Numerical method
Error Finding in Numerical methodError Finding in Numerical method
Error Finding in Numerical method
 
Cloning Bio-Informative
Cloning Bio-InformativeCloning Bio-Informative
Cloning Bio-Informative
 
Amd Athlon Processors
Amd Athlon ProcessorsAmd Athlon Processors
Amd Athlon Processors
 
Data Mining & Applications
Data Mining & ApplicationsData Mining & Applications
Data Mining & Applications
 
Electromagnetic induction & useful applications
Electromagnetic induction & useful applicationsElectromagnetic induction & useful applications
Electromagnetic induction & useful applications
 
Bangladesh Cricket Team.
Bangladesh Cricket Team.Bangladesh Cricket Team.
Bangladesh Cricket Team.
 
Application of mathematics in daily life
Application of mathematics in daily lifeApplication of mathematics in daily life
Application of mathematics in daily life
 
Application of mathematics in daily life
Application of mathematics in daily lifeApplication of mathematics in daily life
Application of mathematics in daily life
 
SIXTH SENSE-TECHNOLOGY
SIXTH SENSE-TECHNOLOGYSIXTH SENSE-TECHNOLOGY
SIXTH SENSE-TECHNOLOGY
 

Dernier

➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...amitlee9823
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...amitlee9823
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...amitlee9823
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteedamy56318795
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...only4webmaster01
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfadriantubila
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...amitlee9823
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 

Dernier (20)

➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 

Structure in C

  • 1. STRUCTURESTRUCTURE in CC programming… Group : ASTUTE Soummo Supriya : 151-15-4741 T.M. Ashikur Rahman : 151-15-4971 Md. Fazle Rabbi Ador : 151-15-5482 Tasnuva Tabassum Oshin : 151-15-4673 Masumer Rahman : 151-15-5040
  • 2. Introduction • Md. Fazle Rabbi adoR • 151-15-5482
  • 3. Structure • A structure is a collection of variables of different data types under a single name. • The variables are called members of the structure. • The structure is also called a user-defined data type. 3
  • 4. Defining a Structure • Syntax: struct structure_name { data_type member_variable1; data_type member_variable2; ………………………………; data_type member_variableN; }; Note: The members of a structure do not occupy memory until they are associated with a structure_variable. 4
  • 5. struct student { char name[20]; int roll_no; float marks; char gender; long int phone_no; }; •Multiple variables of struct student type can be declared as: struct student st1, st2, st3; 5 Example
  • 6. Defining a structure…Defining a structure… • Each variable of structure has its own copy of member variables. • The member variables are accessed using the dot (.) operator or member operator. • For example: st1.name is member variable name of st1 structure variable while st3.gender is member variable gender of st3 structure variable. 6
  • 7. struct student { char name[20]; int ID; float CSE_marks; char gender; long int phone_no; }; void main() { struct student st1={"Ishtiaque",5482,13.5,'M',16021548}; struct student st2={"Oshin",4288,15,'F',19845321}; printf ("Name: %s ID: %d CSE Marks: %.1f Gender: %c Phn: %d n",st1.name,st1.ID,st1.CSE_marks,st1.gender,st1.phone_no); printf ("Name: %s ID: %d CSE Marks: %.1f Gender: %c Phn: %d n",st2.name,st2.ID,st2.CSE_marks,st2.gender,st2.phone_no); } 7
  • 8.
  • 9. Pointers to structurePointers to structure A pointer is a memory address. You can define pointers to structures in very similar way as you define pointer to any other variable Prepared By Soummo Spriya 151-15-4741
  • 11. • Referencing pointer to another address to access memory • Using dynamic memory allocation Structure's member through pointer can be used in two ways:
  • 12.
  • 13. ““Structure within anotherStructure within another StructureStructure (Nested Structure)”(Nested Structure)” Md.Masumer Rahman Id: 151-15-5040
  • 14. What is Nested Structure • Nested structure in C is nothing but structure within structure. One structure can be declared inside other structure as we declare structure members inside a structure. The structure variables can be a normal structure variable or a pointer variable to access the data. • Nested Structures are allowed in C Programming Language. • We can write one Structure inside another structure as member of another structure.
  • 15. Let’s see the structure declaration below,
  • 16. Way 1: Declare two separate structures struct date { int date; int month; int year; }; struct Employee { char ename[20]; int ssn; float salary; struct date doj; } emp1;
  • 17. Way 2 : Declare Embedded structures struct Employee { char ename[20]; int ssn; float salary; struct date { int date; int month; int year; } doj; } emp1;
  • 18.
  • 19. Function and Structure Name: T.m. ashikur rahmaN id: 151-15-4971
  • 20. Function and Structure We will consider four cases here: Passing the individual member to functions  Passing whole structure to functions Passing structure pointer to functions Passing array of structure to functions
  • 21. Passing the individual member to functions  Structure members can be passed to functions as actual arguments in function call like ordinary variables.
  • 22. PASSING WHOLE STRUCTURE TO FUNCTIONS  Whole structure can be passed to a function by the syntax: function _ name ( structure _ variable _ name );  The called function has the form: return _ type function _ name (struct tag _ name structure _ variable _ name) { …………; }
  • 23. Passing structure pointer to functions  In this case, address of structure variable is passed as an actual argument to a function. The corresponding formal argument must be a structure type pointer variable.
  • 24. Passing array of structure to function  Passing an array of structure type to a function is similar to passing an array of any type to a function.  That is, the name of the array of structure is passed by the calling function which is the base address of the array of structure.  Note: The function prototype comes after the structure definition.
  • 25. Arrays of structuresArrays of structures • Tasnuva Tabassum Oshin • 151-15-4673
  • 26. Arrays of structures • An ordinary array: One type of data • An array of structs: Multiple types of data in each array element. 0 1 2 … 98 99 0 1 2 … 98 99
  • 27. Arrays of structures • We often use arrays of structures. • Example: StudentRecord Class[100]; strcpy(Class[98].Name, “bangladeshi man"); Class[98].Id = 12345; strcpy(Class[98].Dept, "COMP"); Class[98].gender = 'M'; Class[0] = Class[98]; Bangladeshi man 12345 M COMP . . . 0 1 2 … 98 99
  • 28. Arrays inside structures • We can use arrays inside structures. • Example: struct square{ point vertex[4]; }; square Sq; • Assign values to Sq using the given square x y x y x y x y (4, 3) (10, 3) (4, 1) (10, 1)