SlideShare a Scribd company logo
Structured Programming Language
Structures in C
Mohammad Imam Hossain,
Lecturer, CSE, UIU
Array vs Structure
• Array: a data structure whose elements are
all of the same data type.
• Structure: elements can differ in type.
i.e. a single structure can contain integer
elements, floating-point elements and
character elements.
Pointers, arrays, other structures can also be
included within a structure.
The individual structure elements are referred
to as members.
Declaring a structure
• struct  keyword
• tag  identifies this type of structure (structure name)
• member 1,member 2, … member m individual member
declaration
struct tag{
member 1;
member 2;
member 3;
...
member m;
};
Declaring a structure
• Individual member can be ordinary
variables, pointers, arrays or other
structures.
• Member names within a particular
structure must be distinct from one
another.
• Member name can be same as the
name of a variable that is defined
outside of the structure.
• Individual members can’t be initialized
within a structure type declaration.
struct tag{
member 1;
member 2;
member 3;
...
member m;
};
Declaring Structure-type variables
struct tag variable_1, variable_2, ... , variable_n;
struct tag{
member 1;
member 2;
member 3;
...
member m;
};
Initializing Structure
struct date{
int day;
int month;
int year;
};
struct date d={13,8,2017};
struct tag variable={value 1, value 2, ... , value m};
• Value 1 refers to the value of first member
• Value 2 refers to the value of second member
Example Code
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct account {
int acct_no;
char acct_type;
char name[80];
float balance;
struct date lastpayment;
};
int main(){
///initializing a structure
struct account my_account={12345,'R',"Ananta",1000.50,13,8,2017};
return 0;
}
Structure member Number of bytes
acct_no 2
acct_type 1
name 80
balance 4
lastpayment 2+2+2
Total 93
Example Code
#include <stdio.h>
struct date{
char name[50];
int day;
int month;
int year;
};
int main(){
///declaring and initializing array of structure
struct date birthday[5]={
{"spiderman",1,1,1994},
{"superman",6,7,1994},
{"batman",15,8,1994},
{"ironman",29,2,2000},
{"hitman",8,10,1994}
};
return 0;
}
Example Code
struct first{
float a;
int b;
char c;
};
struct second{
char a;
float b, c;
};
• Here duplication of member names is permissible, since the
scope of each set of member definitions is confined to its
respective structure.
Processing a Structure
• A structure member can be accessed by writing:
variable . member;
• variable refers to the name of a structure-type
variable
• member refers to the name of a member within the
structure.
• Period (.) is an operator of the highest precedence
group and its associativity is left to right.
Example Code
#include <stdio.h>
struct date{
char name[50];
int day;
int month;
int year;
};
int main(){
struct date d={"Birdman",1,1,1994};
int dayno=d.day;
int monthno=d.month;
int yearno=d.year;
return 0;
}
Structure of Structure
variable . member. submember;
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct account{
int acc_no;
char acc_type;
char name[50];
float balance;
struct date lastpayment;
};
How to access the variable day
from the structure account
Example Code
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct account{
int acc_no;
char acc_type;
char name[50];
float balance;
struct date lastpayment;
};
int main(){
struct account my_acc={123,'R',"Birdman",1000.50,1,1,1994};
int dayno=my_acc.lastpayment.day;
return 0;
}
Example Code
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct account{
int acc_no;
char acc_type;
char name[50];
float balance;
struct date lastpayment;
};
int main(){
struct account my_acc={123,'R',"Birdman",1000.50,1,1,1994};
char firstchar=my_acc.name[0];
return 0;
}
Example Code
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct account{
int acc_no;
char acc_type;
char name[50];
float balance;
struct date lastpayment;
};
int main(){
struct account my_acc1={123,'R',"Birdman",1000.50,1,1,1994};
struct account my_acc2;
my_acc2=my_acc1;///copy the values of my_acc1 to my_acc2
return 0;
}
References
• Programming with C, schaum’s outlines, 3rd edition
chapter 12: section 12.1 and 12.2 (programming
example 12.14)

More Related Content

Similar to SPL 14 | Structures in C

User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
Ananthi Palanisamy
 
Structure.pptx
Structure.pptxStructure.pptx
Structure.pptx
MohammedOmer401579
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
Shankar Gangaju
 
SPC Unit 5
SPC Unit 5SPC Unit 5
SPC Unit 5
SIMONTHOMAS S
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
Prerna Sharma
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
mrecedu
 
Str
StrStr
Str
Acad
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 
Structure in C
Structure in CStructure in C
Structure in C
Fazle Rabbi Ador
 
L6 structure
L6 structureL6 structure
L6 structure
mondalakash2012
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdf
HimanshuKansal22
 
Structure c
Structure cStructure c
Structure c
thirumalaikumar3
 
Structures in C
Structures in CStructures in C
Structures in C
Nimrita Koul
 
Structure & union
Structure & unionStructure & union
Structure & union
lalithambiga kamaraj
 
1. structure
1. structure1. structure
1. structure
hasan Mohammad
 
Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
TAlha MAlik
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
Sowri Rajan
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
Prabu U
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
Ashim Lamichhane
 

Similar to SPL 14 | Structures in C (20)

User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
Structure.pptx
Structure.pptxStructure.pptx
Structure.pptx
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
 
SPC Unit 5
SPC Unit 5SPC Unit 5
SPC Unit 5
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
Str
StrStr
Str
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Structure in C
Structure in CStructure in C
Structure in C
 
L6 structure
L6 structureL6 structure
L6 structure
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdf
 
Structure c
Structure cStructure c
Structure c
 
Structures in C
Structures in CStructures in C
Structures in C
 
Structure & union
Structure & unionStructure & union
Structure & union
 
1. structure
1. structure1. structure
1. structure
 
Structure in C
Structure in CStructure in C
Structure in C
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 

More from Mohammad Imam Hossain

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
Mohammad Imam Hossain
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
Mohammad Imam Hossain
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
Mohammad Imam Hossain
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
Mohammad Imam Hossain
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
Mohammad Imam Hossain
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
Mohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
Mohammad Imam Hossain
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
Mohammad Imam Hossain
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
Mohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
Mohammad Imam Hossain
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
Mohammad Imam Hossain
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
Mohammad Imam Hossain
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
Mohammad Imam Hossain
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
Mohammad Imam Hossain
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
Mohammad Imam Hossain
 

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
 

Recently uploaded

BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptxContiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Kalna College
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
Prof. Dr. K. Adisesha
 
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
220711130083 SUBHASHREE RAKSHIT  Internet resources for social science220711130083 SUBHASHREE RAKSHIT  Internet resources for social science
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
Kalna College
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
IsmaelVazquez38
 
BPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end examBPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end exam
sonukumargpnirsadhan
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
Celine George
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
sanamushtaq922
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
khuleseema60
 
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptxA Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
OH TEIK BIN
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
Payaamvohra1
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
RamseyBerglund
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
indexPub
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
Nguyen Thanh Tu Collection
 
How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17
Celine George
 

Recently uploaded (20)

BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptxContiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptx
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
 
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
220711130083 SUBHASHREE RAKSHIT  Internet resources for social science220711130083 SUBHASHREE RAKSHIT  Internet resources for social science
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
 
BPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end examBPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end exam
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
 
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptxA Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
 
How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17
 

SPL 14 | Structures in C

  • 1. Structured Programming Language Structures in C Mohammad Imam Hossain, Lecturer, CSE, UIU
  • 2. Array vs Structure • Array: a data structure whose elements are all of the same data type. • Structure: elements can differ in type. i.e. a single structure can contain integer elements, floating-point elements and character elements. Pointers, arrays, other structures can also be included within a structure. The individual structure elements are referred to as members.
  • 3. Declaring a structure • struct  keyword • tag  identifies this type of structure (structure name) • member 1,member 2, … member m individual member declaration struct tag{ member 1; member 2; member 3; ... member m; };
  • 4. Declaring a structure • Individual member can be ordinary variables, pointers, arrays or other structures. • Member names within a particular structure must be distinct from one another. • Member name can be same as the name of a variable that is defined outside of the structure. • Individual members can’t be initialized within a structure type declaration. struct tag{ member 1; member 2; member 3; ... member m; };
  • 5. Declaring Structure-type variables struct tag variable_1, variable_2, ... , variable_n; struct tag{ member 1; member 2; member 3; ... member m; };
  • 6. Initializing Structure struct date{ int day; int month; int year; }; struct date d={13,8,2017}; struct tag variable={value 1, value 2, ... , value m}; • Value 1 refers to the value of first member • Value 2 refers to the value of second member
  • 7. Example Code #include <stdio.h> struct date{ int day; int month; int year; }; struct account { int acct_no; char acct_type; char name[80]; float balance; struct date lastpayment; }; int main(){ ///initializing a structure struct account my_account={12345,'R',"Ananta",1000.50,13,8,2017}; return 0; } Structure member Number of bytes acct_no 2 acct_type 1 name 80 balance 4 lastpayment 2+2+2 Total 93
  • 8. Example Code #include <stdio.h> struct date{ char name[50]; int day; int month; int year; }; int main(){ ///declaring and initializing array of structure struct date birthday[5]={ {"spiderman",1,1,1994}, {"superman",6,7,1994}, {"batman",15,8,1994}, {"ironman",29,2,2000}, {"hitman",8,10,1994} }; return 0; }
  • 9. Example Code struct first{ float a; int b; char c; }; struct second{ char a; float b, c; }; • Here duplication of member names is permissible, since the scope of each set of member definitions is confined to its respective structure.
  • 10. Processing a Structure • A structure member can be accessed by writing: variable . member; • variable refers to the name of a structure-type variable • member refers to the name of a member within the structure. • Period (.) is an operator of the highest precedence group and its associativity is left to right.
  • 11. Example Code #include <stdio.h> struct date{ char name[50]; int day; int month; int year; }; int main(){ struct date d={"Birdman",1,1,1994}; int dayno=d.day; int monthno=d.month; int yearno=d.year; return 0; }
  • 12. Structure of Structure variable . member. submember; #include <stdio.h> struct date{ int day; int month; int year; }; struct account{ int acc_no; char acc_type; char name[50]; float balance; struct date lastpayment; }; How to access the variable day from the structure account
  • 13. Example Code #include <stdio.h> struct date{ int day; int month; int year; }; struct account{ int acc_no; char acc_type; char name[50]; float balance; struct date lastpayment; }; int main(){ struct account my_acc={123,'R',"Birdman",1000.50,1,1,1994}; int dayno=my_acc.lastpayment.day; return 0; }
  • 14. Example Code #include <stdio.h> struct date{ int day; int month; int year; }; struct account{ int acc_no; char acc_type; char name[50]; float balance; struct date lastpayment; }; int main(){ struct account my_acc={123,'R',"Birdman",1000.50,1,1,1994}; char firstchar=my_acc.name[0]; return 0; }
  • 15. Example Code #include <stdio.h> struct date{ int day; int month; int year; }; struct account{ int acc_no; char acc_type; char name[50]; float balance; struct date lastpayment; }; int main(){ struct account my_acc1={123,'R',"Birdman",1000.50,1,1,1994}; struct account my_acc2; my_acc2=my_acc1;///copy the values of my_acc1 to my_acc2 return 0; }
  • 16. References • Programming with C, schaum’s outlines, 3rd edition chapter 12: section 12.1 and 12.2 (programming example 12.14)