SlideShare a Scribd company logo
1
C Programming for
Engineers
Structures, Unions
ICEN 360– Spring 2017
Prof. Dola Saha
2
Structure
Ø Collections of related variables under one name.
Ø Variables of may be of different data types.
Ø struct card {
char *face;
char *suit;
};
Ø Keyword struct introduces the structure definition.
Ø Members of the same structure type must have unique
names, but two different structure types may contain
members of the same name without conflict.
Tag
Members
3
Structure Declaration
Ø struct employee {
char firstName[20];
char lastName[20];
unsigned int age;
char gender;
double hourlySalary;
};
Ø struct employee employee1, employee2;
Ø struct employee employees[100];
Ø struct employee {
char firstName[20];
char lastName[20];
unsigned int age;
char gender;
double hourlySalary;
} employee1, employee2, *employeePtr;
4
Structure Tag
Ø The structure tag name is optional.
Ø If a structure definition does not contain a structure tag
name, variables of the structure type may be declared
only in the structure definition—not in a separate
declaration.
5
Self Reference
Ø A structure cannot contain an instance of itself.
Ø A variable of type struct employee cannot be declared in the
definition for struct employee.
Ø A pointer to struct employee, may be included.
Ø For example,
o struct employee2 {
char firstName[20];
char lastName[20];
unsigned int age;
char gender;
double hourlySalary;
struct employee2 person; // ERROR
struct employee2 *ePtr; // pointer
};
Ø struct employee2 contains an instance of itself (person), which is an
error.
6
Storage in Memory
Ø Structures may not be compared using operators == and
!=, because
§ structure members are not necessarily stored in consecutive bytes of
memory.
Ø Computers may store specific data types only on certain
memory boundaries such as half-word, word or double-
word boundaries.
Ø A word is a standard memory unit used to store data in a
computer—usually 2 bytes or 4 bytes.
7
Storage in Memory
Ø struct example {
char c;
int i;
} sample1, sample2;
Possible storage,but machine dependant
8
Initialization
Ø struct card {
char *face;
char *suit;
};
Ø struct card aCard = {"Three", "Hearts"};
Ø If there are fewer initializers in the list than members in
the structure,
§ the remaining members are automatically initialized to 0
§ or NULL if the member is a pointer.
Ø Assignment Statement of same struct type
§ struct card aCard1 = aCard2;
9
Accessing Structure Members
Ø the structure member operator (.)—also called the dot
operator
§ printf("%s", aCard.suit); // displays
Hearts
Ø the structure pointer operator (->)—also called the arrow
operator.
§ cardPtr = &aCard;
§ printf("%s", cardPtr->suit); // displays
Hearts
§ Following are equivalent
o cardPtr->suit
o (*cardPtr).suit
10
Example
11
Structure with Function
Ø Structures may be passed to functions by
§ passing individual structure members
§ by passing an entire structure
§ by passing a pointer to a structure.
Ø Functions can return
§ individual structure members
§ an entire structure
§ a pointer to a structure
12
typedef
Ø The keyword typedef is a way to create synonyms (or
aliases) for previously defined data types.
Ø Names for structure types are often defined with typedef
to create shorter type names.
Ø Example:
§ typedef struct card Card;
Card is a synonym for type struct card.
Ø Example:
§ typedef struct {
char *face;
char *suit;
} Card;
§ Card myCard, *myCardPtr, deck[52];
13
Card Shuffling Example (1)
14
Card Shuffling Example (2)
15
Card Shuffling Example (3)
16
Card Shuffling Example (4)
17
Card Shuffling Example (5)
18
Classwork Assignment
Ø Write a program to generate data for N students. Use
structure to create numeric ID and points (max 100) as 2
separate members. Randomly generate data for N
students. Display both the ID and the points of the
student who has received highest point.
19
Union
Ø A union is a derived data type—like a structure—with
members that share the same storage space.
Ø For different situations in a program, some variables may
not be relevant, but other variables are—so a union
shares the space instead of wasting storage on variables
that are not being used.
Ø The members of a union can be of any data type.
Ø The number of bytes used to store a union must be at
least enough to hold the largest member.
20
Definition
Ø union number {
int x;
double y;
};
Ø In a declaration, a union may be initialized with a value of the same
type as the first union member.
Ø union number value = {10};
Ø union number value = {1.43}; // ERROR
21
Permitted Operations
Ø The operations that can be performed on a union are:
§ assigning a union to another union of the same type,
§ taking the address (&) of a union variable,
§ and accessing union members using the structure member operator
and the structure pointer operator.
Ø Unions may not be compared using operators == and !=
for the same reasons that structures cannot be compared.
22
Union Example (1)
23
Union Example (2)
24
Enumeration
Ø Keyword enum, is a set of integer enumeration constants
represented by identifiers.
Ø Values in an enum start with 0, unless specified otherwise, and are
incremented by 1.
Ø For example, the enumeration
o enum months {
JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP,
OCT, NOV, DEC};
creates a new type, enum months, identifiers are set to the
integers 0 to 11, respectively.
Ø Example:
§ enum months {
JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG,
SEP, OCT, NOV, DEC};
identifiers are set to integers 1 to 12, respectively.
25
Enumeration Example
26
Enumeration Example Output

More Related Content

Similar to lec14.pdf

Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
Vijayananda Ratnam Ch
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
Ram Sagar Mourya
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
Prerna Sharma
 
structures and unions in 'C'
structures and unions in 'C'structures and unions in 'C'
structures and unions in 'C'
illpa
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9patcha535
 
Structure In C
Structure In CStructure In C
Structure In C
yndaravind
 
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
meharikiros2
 
Structure & union
Structure & unionStructure & union
Structure & union
lalithambiga kamaraj
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
Krishna Nanda
 
Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 
1. structure
1. structure1. structure
1. structure
hasan Mohammad
 
C structure and union
C structure and unionC structure and union
C structure and union
Thesis Scientist Private Limited
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
sangrampatil81
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
Shankar Gangaju
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
Tanmay Modi
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
tanmaymodi4
 
Structures
StructuresStructures
Structures
arshpreetkaur07
 
Fundamentals of Structure in C Programming
Fundamentals of Structure in C ProgrammingFundamentals of Structure in C Programming
Fundamentals of Structure in C Programming
Chandrakant Divate
 

Similar to lec14.pdf (20)

Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 
structures and unions in 'C'
structures and unions in 'C'structures and unions in 'C'
structures and unions in 'C'
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
Structure In C
Structure In CStructure In C
Structure In C
 
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Structure in C
Structure in CStructure in C
Structure in C
 
1. structure
1. structure1. structure
1. structure
 
C structure and union
C structure and unionC structure and union
C structure and union
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures
StructuresStructures
Structures
 
Fundamentals of Structure in C Programming
Fundamentals of Structure in C ProgrammingFundamentals of Structure in C Programming
Fundamentals of Structure in C Programming
 
Structure
StructureStructure
Structure
 

Recently uploaded

block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 

Recently uploaded (20)

block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 

lec14.pdf

  • 1. 1 C Programming for Engineers Structures, Unions ICEN 360– Spring 2017 Prof. Dola Saha
  • 2. 2 Structure Ø Collections of related variables under one name. Ø Variables of may be of different data types. Ø struct card { char *face; char *suit; }; Ø Keyword struct introduces the structure definition. Ø Members of the same structure type must have unique names, but two different structure types may contain members of the same name without conflict. Tag Members
  • 3. 3 Structure Declaration Ø struct employee { char firstName[20]; char lastName[20]; unsigned int age; char gender; double hourlySalary; }; Ø struct employee employee1, employee2; Ø struct employee employees[100]; Ø struct employee { char firstName[20]; char lastName[20]; unsigned int age; char gender; double hourlySalary; } employee1, employee2, *employeePtr;
  • 4. 4 Structure Tag Ø The structure tag name is optional. Ø If a structure definition does not contain a structure tag name, variables of the structure type may be declared only in the structure definition—not in a separate declaration.
  • 5. 5 Self Reference Ø A structure cannot contain an instance of itself. Ø A variable of type struct employee cannot be declared in the definition for struct employee. Ø A pointer to struct employee, may be included. Ø For example, o struct employee2 { char firstName[20]; char lastName[20]; unsigned int age; char gender; double hourlySalary; struct employee2 person; // ERROR struct employee2 *ePtr; // pointer }; Ø struct employee2 contains an instance of itself (person), which is an error.
  • 6. 6 Storage in Memory Ø Structures may not be compared using operators == and !=, because § structure members are not necessarily stored in consecutive bytes of memory. Ø Computers may store specific data types only on certain memory boundaries such as half-word, word or double- word boundaries. Ø A word is a standard memory unit used to store data in a computer—usually 2 bytes or 4 bytes.
  • 7. 7 Storage in Memory Ø struct example { char c; int i; } sample1, sample2; Possible storage,but machine dependant
  • 8. 8 Initialization Ø struct card { char *face; char *suit; }; Ø struct card aCard = {"Three", "Hearts"}; Ø If there are fewer initializers in the list than members in the structure, § the remaining members are automatically initialized to 0 § or NULL if the member is a pointer. Ø Assignment Statement of same struct type § struct card aCard1 = aCard2;
  • 9. 9 Accessing Structure Members Ø the structure member operator (.)—also called the dot operator § printf("%s", aCard.suit); // displays Hearts Ø the structure pointer operator (->)—also called the arrow operator. § cardPtr = &aCard; § printf("%s", cardPtr->suit); // displays Hearts § Following are equivalent o cardPtr->suit o (*cardPtr).suit
  • 11. 11 Structure with Function Ø Structures may be passed to functions by § passing individual structure members § by passing an entire structure § by passing a pointer to a structure. Ø Functions can return § individual structure members § an entire structure § a pointer to a structure
  • 12. 12 typedef Ø The keyword typedef is a way to create synonyms (or aliases) for previously defined data types. Ø Names for structure types are often defined with typedef to create shorter type names. Ø Example: § typedef struct card Card; Card is a synonym for type struct card. Ø Example: § typedef struct { char *face; char *suit; } Card; § Card myCard, *myCardPtr, deck[52];
  • 18. 18 Classwork Assignment Ø Write a program to generate data for N students. Use structure to create numeric ID and points (max 100) as 2 separate members. Randomly generate data for N students. Display both the ID and the points of the student who has received highest point.
  • 19. 19 Union Ø A union is a derived data type—like a structure—with members that share the same storage space. Ø For different situations in a program, some variables may not be relevant, but other variables are—so a union shares the space instead of wasting storage on variables that are not being used. Ø The members of a union can be of any data type. Ø The number of bytes used to store a union must be at least enough to hold the largest member.
  • 20. 20 Definition Ø union number { int x; double y; }; Ø In a declaration, a union may be initialized with a value of the same type as the first union member. Ø union number value = {10}; Ø union number value = {1.43}; // ERROR
  • 21. 21 Permitted Operations Ø The operations that can be performed on a union are: § assigning a union to another union of the same type, § taking the address (&) of a union variable, § and accessing union members using the structure member operator and the structure pointer operator. Ø Unions may not be compared using operators == and != for the same reasons that structures cannot be compared.
  • 24. 24 Enumeration Ø Keyword enum, is a set of integer enumeration constants represented by identifiers. Ø Values in an enum start with 0, unless specified otherwise, and are incremented by 1. Ø For example, the enumeration o enum months { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; creates a new type, enum months, identifiers are set to the integers 0 to 11, respectively. Ø Example: § enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; identifiers are set to integers 1 to 12, respectively.