SlideShare a Scribd company logo
1 of 41
Download to read offline
Lecture 7
C
Language
ENG : HATEM ABD EL-SALAM
• Structure
• Unions
• Enums
Agenda
Structure
• Structure is a collection of variables of different types under a single name
• If you want to store some information about a person: his/her name, citizenship
number and salary. You can easily create different variables name, citNo, salary to
store these information separately.
• However, in the future, you would want to store information about multiple persons.
Now, you'd need to create different variables for each information per
person: name1, citNo1, salary1, name2, citNo2, salary2.
• You can easily visualize how big and messy the code would look. Also, since no
relation between the variables (information) would exist, it's going to be a daunting
task.
• A better approach will be to have a collection of all related information under a
single name Person, and use it for every person. Now, the code looks much cleaner,
readable and efficient as well.
Structure (Cont.)
• Keyword struct is used for creating a structure.
• Note: Don't forget the semicolon }; in the ending line.
• We can create the structure for a person as mentioned above as:
Structure (Cont.)
Structure variable declaration
• For the above structure of a person, variable can be declared as:
Structure (Cont.)
Structure variable declaration
• Another way of creating a structure variable is:
Structure (Cont.)
Structure variable declaration
• In both cases, two variables person1, person2 and an
array person3 having 20 elements of type struct person are created.
• Note: When a structure is defined, it creates a user-defined type but,
no storage or memory is allocated.
Structure (Cont.)
Accessing members of a structure
• There are two types of operators used for accessing members of a
structure.
1. Member operator(.)
2. Structure pointer operator(->)
Structure (Cont.)
1- Member operator(.)
• Any member of a structure can be accessed as:
• Suppose, we want to access salary for variable person2. Then, it can be
accessed as:
Structure (Cont.)
2-Structure pointer operator(->)
• Structures can be created and accessed using pointers. A pointer
variable of a structure can be created as below:
• Here, the pointer variable of type struct name is created.
Structure (Cont.)
A structure's member can be accesssed through pointer in two
ways:
1. Referencing pointer to another address to access memory
2. Using dynamic memory allocation
Structure (Cont.)
1- Referencing pointer
In this example, the pointer variable of type struct person is referenced to the address of person1.
Then, only the structure member through pointer can accessed.
Structure (Cont.)
1- Referencing pointer
Using -> operator
• Structure pointer member can also be accessed using -> operator.
Structure (Cont.)
1- dynamic memory
• To access structure member using pointers, memory can be allocated dynamically using
malloc() function defined under "stdlib.h" header file.
Structure (Cont.)
• Structures within structures
• Structures can be nested within other structures in C programming.
• Suppose, you want to access imag_value for num2 structure variable then, following
structure member is used.
Structure (Cont.)
Passing structures to a function
There are mainly two ways to pass structures to a function:
1. Passing by value
2. Passing by reference
Structure (Cont.)
Passing by value
• A structure variable can be passed to the function as an argument as a normal
variable.
• If structure is passed by value, changes made to the structure variable inside the
function definition does not reflect in the originally passed structure variable.
Structure (Cont.)
Structure (Cont.)
Passing by reference
• The memory address of a structure variable is passed to function while passing it by
reference.
• If structure is passed by reference, changes made to the structure variable inside
function definition reflects in the originally passed structure variable.
EXERCISES
Exercises
• Calculate Difference Between Two Time Period
• Store Information of 10 Students Using Structure
Unions
• Unions are quite similar to structures. Like structures, unions are also derived
types.
• Defining a union is as easy as replacing the keyword struct with the
keyword union.
Unions
• Union variables can be created in similar manner as structure variables.
OR
• In both cases, union variables car1, car2 and union pointer variable car3 of type
union car is created.
Unions
Accessing members of a union
• Again, the member of unions can be accessed in similar manner as structures.
• Using pointer.
Unions
Passing Union To a Function
• Union can be passed in similar manner as structures in C programming.
Unions
Difference between union and structure
• Though unions are similar to structure in so many ways, the difference between
them is crucial to understand
• The primary difference can be demonstrated by this example:
Unions
Difference between union and structure
• As seen in the above example, there is a difference in memory allocation between
union and structure.
• The amount of memory required to store a structure variable is the sum of memory
size of all members.
• But, the memory required to store a union variable is the memory required for the
largest element of an union.
Unions
• only one of its members can be accessed at a time and all other members will
contain garbage values.
Enumeration
• An enumeration is a user-defined data type that consists of integral
constants. To define an enumeration, keyword enum is used.
• Here, name of the enumeration is flag.
• And, const1, const2,...., constN are values of type flag.
• By default, const1 is 0, const2 is 1 and so on. You can change default values of
enum elements during declaration (if necessary).
Enumeration (Cont.)
• When you create an enumerated type, only blueprint for the variable is
created. Here's how you can create variables of enum type.
• Here, a variable check of type enum boolean is created.
• another way to declare same check variable using different syntax
Enumeration (Cont.)
• Changing default values of enum.
Enumeration (Cont.)
• Example1:
Enumeration (Cont.)
• Example2:
Enumeration (Cont.)
• Example3:
C- language Lecture 7

More Related Content

What's hot

220 runtime environments
220 runtime environments220 runtime environments
220 runtime environmentsJ'tong Atong
 
Polymorphism
PolymorphismPolymorphism
PolymorphismAmir Ali
 
9 subprograms
9 subprograms9 subprograms
9 subprogramsjigeno
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Gamindu Udayanga
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)Dilawar Khan
 
11 abstract data types
11 abstract data types11 abstract data types
11 abstract data typesjigeno
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4sumitbardhan
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its typesSuraj Bora
 
Cityu Appslab c++ talk
Cityu Appslab c++ talkCityu Appslab c++ talk
Cityu Appslab c++ talkPeter Cheung
 
Cobol questions and answers
Cobol questions and answersCobol questions and answers
Cobol questions and answersSweta Singh
 

What's hot (20)

C++ Version 2
C++  Version 2C++  Version 2
C++ Version 2
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environments
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
 
098ca session7 c++
098ca session7 c++098ca session7 c++
098ca session7 c++
 
Pointers
PointersPointers
Pointers
 
C++ version 1
C++  version 1C++  version 1
C++ version 1
 
9. control statement
9. control statement9. control statement
9. control statement
 
9 Inputs & Outputs
9 Inputs & Outputs9 Inputs & Outputs
9 Inputs & Outputs
 
Unit 4
Unit 4Unit 4
Unit 4
 
11 abstract data types
11 abstract data types11 abstract data types
11 abstract data types
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its types
 
Cityu Appslab c++ talk
Cityu Appslab c++ talkCityu Appslab c++ talk
Cityu Appslab c++ talk
 
Cobol questions and answers
Cobol questions and answersCobol questions and answers
Cobol questions and answers
 
Structure
StructureStructure
Structure
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 

Similar to C- language Lecture 7

Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and UnionsAshim Lamichhane
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and UnionSelvaraj Seerangan
 
Advance database system(part 4)
Advance database system(part 4)Advance database system(part 4)
Advance database system(part 4)Abdullah Khosa
 
Union In language C
Union In language CUnion In language C
Union In language CRavi Singh
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptxSKUP1
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptxLECO9
 
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptxUnit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptxvekariyakashyap
 
Structures unions
Structures  unionsStructures  unions
Structures unionsSugnan M
 
Unit 2_DBMS_10.2.22.pptx
Unit 2_DBMS_10.2.22.pptxUnit 2_DBMS_10.2.22.pptx
Unit 2_DBMS_10.2.22.pptxMaryJoseph79
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programmingKamal Acharya
 
10-System-ModelingFL22-sketch-19122022-091234am.pptx
10-System-ModelingFL22-sketch-19122022-091234am.pptx10-System-ModelingFL22-sketch-19122022-091234am.pptx
10-System-ModelingFL22-sketch-19122022-091234am.pptxhuzaifaahmed79
 
Chapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptChapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptRushikeshChikane1
 
Chapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptChapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptRushikeshChikane2
 
ICT DBA3 09 0710 Model Data Objects.pdf
ICT DBA3 09 0710 Model Data Objects.pdfICT DBA3 09 0710 Model Data Objects.pdf
ICT DBA3 09 0710 Model Data Objects.pdfInfotech27
 

Similar to C- language Lecture 7 (20)

Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
Chapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptxChapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptx
 
Advance database system(part 4)
Advance database system(part 4)Advance database system(part 4)
Advance database system(part 4)
 
Union In language C
Union In language CUnion In language C
Union In language C
 
Structure
StructureStructure
Structure
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptxUnit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
 
Structures unions
Structures  unionsStructures  unions
Structures unions
 
Unit 2_DBMS_10.2.22.pptx
Unit 2_DBMS_10.2.22.pptxUnit 2_DBMS_10.2.22.pptx
Unit 2_DBMS_10.2.22.pptx
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
10-System-ModelingFL22-sketch-19122022-091234am.pptx
10-System-ModelingFL22-sketch-19122022-091234am.pptx10-System-ModelingFL22-sketch-19122022-091234am.pptx
10-System-ModelingFL22-sketch-19122022-091234am.pptx
 
Chapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptChapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.ppt
 
Chapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptChapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.ppt
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
ICT DBA3 09 0710 Model Data Objects.pdf
ICT DBA3 09 0710 Model Data Objects.pdfICT DBA3 09 0710 Model Data Objects.pdf
ICT DBA3 09 0710 Model Data Objects.pdf
 
CPP19 - Revision
CPP19 - RevisionCPP19 - Revision
CPP19 - Revision
 

More from Hatem Abd El-Salam

More from Hatem Abd El-Salam (13)

Java- language Lecture 7
Java- language Lecture 7Java- language Lecture 7
Java- language Lecture 7
 
Java- language Lecture 6
Java- language Lecture 6Java- language Lecture 6
Java- language Lecture 6
 
Java- language Lecture 5
Java- language Lecture 5Java- language Lecture 5
Java- language Lecture 5
 
Java- language Lecture 4
Java- language Lecture 4Java- language Lecture 4
Java- language Lecture 4
 
Java- language Lecture 3
Java- language Lecture 3Java- language Lecture 3
Java- language Lecture 3
 
Java- Language Lecture 2
Java- Language Lecture 2Java- Language Lecture 2
Java- Language Lecture 2
 
Java- language Lecture 1
Java- language Lecture 1Java- language Lecture 1
Java- language Lecture 1
 
introduction to embedded systems part 2
introduction to embedded systems part 2introduction to embedded systems part 2
introduction to embedded systems part 2
 
introduction to embedded systems part 1
introduction to embedded systems part 1introduction to embedded systems part 1
introduction to embedded systems part 1
 
C- language Lecture 6
C- language Lecture 6C- language Lecture 6
C- language Lecture 6
 
C- language Lecture 3
C- language Lecture 3C- language Lecture 3
C- language Lecture 3
 
C- Language Lecture 2
C- Language Lecture 2C- Language Lecture 2
C- Language Lecture 2
 
C-language Lecture 1
C-language Lecture 1C-language Lecture 1
C-language Lecture 1
 

Recently uploaded

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

C- language Lecture 7

  • 1. Lecture 7 C Language ENG : HATEM ABD EL-SALAM
  • 3. Structure • Structure is a collection of variables of different types under a single name • If you want to store some information about a person: his/her name, citizenship number and salary. You can easily create different variables name, citNo, salary to store these information separately. • However, in the future, you would want to store information about multiple persons. Now, you'd need to create different variables for each information per person: name1, citNo1, salary1, name2, citNo2, salary2. • You can easily visualize how big and messy the code would look. Also, since no relation between the variables (information) would exist, it's going to be a daunting task. • A better approach will be to have a collection of all related information under a single name Person, and use it for every person. Now, the code looks much cleaner, readable and efficient as well.
  • 4. Structure (Cont.) • Keyword struct is used for creating a structure. • Note: Don't forget the semicolon }; in the ending line. • We can create the structure for a person as mentioned above as:
  • 5. Structure (Cont.) Structure variable declaration • For the above structure of a person, variable can be declared as:
  • 6. Structure (Cont.) Structure variable declaration • Another way of creating a structure variable is:
  • 7. Structure (Cont.) Structure variable declaration • In both cases, two variables person1, person2 and an array person3 having 20 elements of type struct person are created. • Note: When a structure is defined, it creates a user-defined type but, no storage or memory is allocated.
  • 8. Structure (Cont.) Accessing members of a structure • There are two types of operators used for accessing members of a structure. 1. Member operator(.) 2. Structure pointer operator(->)
  • 9. Structure (Cont.) 1- Member operator(.) • Any member of a structure can be accessed as: • Suppose, we want to access salary for variable person2. Then, it can be accessed as:
  • 10.
  • 11. Structure (Cont.) 2-Structure pointer operator(->) • Structures can be created and accessed using pointers. A pointer variable of a structure can be created as below: • Here, the pointer variable of type struct name is created.
  • 12. Structure (Cont.) A structure's member can be accesssed through pointer in two ways: 1. Referencing pointer to another address to access memory 2. Using dynamic memory allocation
  • 13. Structure (Cont.) 1- Referencing pointer In this example, the pointer variable of type struct person is referenced to the address of person1. Then, only the structure member through pointer can accessed.
  • 14. Structure (Cont.) 1- Referencing pointer Using -> operator • Structure pointer member can also be accessed using -> operator.
  • 15. Structure (Cont.) 1- dynamic memory • To access structure member using pointers, memory can be allocated dynamically using malloc() function defined under "stdlib.h" header file.
  • 16.
  • 17. Structure (Cont.) • Structures within structures • Structures can be nested within other structures in C programming. • Suppose, you want to access imag_value for num2 structure variable then, following structure member is used.
  • 18. Structure (Cont.) Passing structures to a function There are mainly two ways to pass structures to a function: 1. Passing by value 2. Passing by reference
  • 19. Structure (Cont.) Passing by value • A structure variable can be passed to the function as an argument as a normal variable. • If structure is passed by value, changes made to the structure variable inside the function definition does not reflect in the originally passed structure variable.
  • 21. Structure (Cont.) Passing by reference • The memory address of a structure variable is passed to function while passing it by reference. • If structure is passed by reference, changes made to the structure variable inside function definition reflects in the originally passed structure variable.
  • 22.
  • 24. Exercises • Calculate Difference Between Two Time Period • Store Information of 10 Students Using Structure
  • 25.
  • 26. Unions • Unions are quite similar to structures. Like structures, unions are also derived types. • Defining a union is as easy as replacing the keyword struct with the keyword union.
  • 27. Unions • Union variables can be created in similar manner as structure variables. OR • In both cases, union variables car1, car2 and union pointer variable car3 of type union car is created.
  • 28. Unions Accessing members of a union • Again, the member of unions can be accessed in similar manner as structures. • Using pointer.
  • 29. Unions Passing Union To a Function • Union can be passed in similar manner as structures in C programming.
  • 30. Unions Difference between union and structure • Though unions are similar to structure in so many ways, the difference between them is crucial to understand • The primary difference can be demonstrated by this example:
  • 31.
  • 32. Unions Difference between union and structure • As seen in the above example, there is a difference in memory allocation between union and structure. • The amount of memory required to store a structure variable is the sum of memory size of all members. • But, the memory required to store a union variable is the memory required for the largest element of an union.
  • 33. Unions • only one of its members can be accessed at a time and all other members will contain garbage values.
  • 34.
  • 35. Enumeration • An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used. • Here, name of the enumeration is flag. • And, const1, const2,...., constN are values of type flag. • By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements during declaration (if necessary).
  • 36. Enumeration (Cont.) • When you create an enumerated type, only blueprint for the variable is created. Here's how you can create variables of enum type. • Here, a variable check of type enum boolean is created. • another way to declare same check variable using different syntax
  • 37. Enumeration (Cont.) • Changing default values of enum.