SlideShare a Scribd company logo
1 of 16
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

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 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 SearchMohammad 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 IntroductionMohammad 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 SchemaMohammad 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 SchemaMohammad 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 CheckMohammad 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

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
 
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...Amil baba
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
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.pptxMaritesTamaniVerdade
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
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.pptxAreebaZafar22
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
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.pdfAdmir Softic
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
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 17Celine George
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 

Recently uploaded (20)

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...
 
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...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
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
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
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
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
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
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain 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)