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

C++ Version 2
C++  Version 2C++  Version 2
C++ Version 2
JIGAR MAKHIJA
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environments
J'tong Atong
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Amir Ali
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
jigeno
 
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
 
098ca session7 c++
098ca session7 c++098ca session7 c++
098ca session7 c++
Mukund Trivedi
 
Pointers
PointersPointers
C++ version 1
C++  version 1C++  version 1
C++ version 1
JIGAR MAKHIJA
 
9. control statement
9. control statement9. control statement
9. control statement
Zambales National High School
 
9 Inputs & Outputs
9 Inputs & Outputs9 Inputs & Outputs
9 Inputs & Outputs
Deepak Hagadur Bheemaraju
 
Unit 4
Unit 4Unit 4
11 abstract data types
11 abstract data types11 abstract data types
11 abstract data types
jigeno
 
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
sumitbardhan
 
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
sumitbardhan
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its types
Suraj Bora
 
Cityu Appslab c++ talk
Cityu Appslab c++ talkCityu Appslab c++ talk
Cityu Appslab c++ talk
Peter Cheung
 
Cobol questions and answers
Cobol questions and answersCobol questions and answers
Cobol questions and answers
Sweta Singh
 
Structure
StructureStructure
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
Prof. Dr. K. Adisesha
 

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 Unions
Ashim Lamichhane
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
Selvaraj Seerangan
 
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
Abhishekkumarsingh630054
 
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 C
Ravi Singh
 
Structure
StructureStructure
Structure
Frijo Francis
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
SKUP1
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
LECO9
 
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
vekariyakashyap
 
Structures unions
Structures  unionsStructures  unions
Structures unions
Sugnan 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.pptx
MaryJoseph79
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal 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.pptx
huzaifaahmed79
 
Chapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptChapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.ppt
RushikeshChikane1
 
Chapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptChapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.ppt
RushikeshChikane2
 
Programming in C
Programming in CProgramming in C
Programming in C
MalathiNagarajan20
 
Design patterns
Design patternsDesign patterns
Design patterns
Jean Pаoli
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
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
Infotech27
 
CPP19 - Revision
CPP19 - RevisionCPP19 - Revision
CPP19 - Revision
Michael Heron
 

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

Java- language Lecture 7
Java- language Lecture 7Java- language Lecture 7
Java- language Lecture 7
Hatem Abd El-Salam
 
Java- language Lecture 6
Java- language Lecture 6Java- language Lecture 6
Java- language Lecture 6
Hatem Abd El-Salam
 
Java- language Lecture 5
Java- language Lecture 5Java- language Lecture 5
Java- language Lecture 5
Hatem Abd El-Salam
 
Java- language Lecture 4
Java- language Lecture 4Java- language Lecture 4
Java- language Lecture 4
Hatem Abd El-Salam
 
Java- language Lecture 3
Java- language Lecture 3Java- language Lecture 3
Java- language Lecture 3
Hatem Abd El-Salam
 
Java- Language Lecture 2
Java- Language Lecture 2Java- Language Lecture 2
Java- Language Lecture 2
Hatem Abd El-Salam
 
Java- language Lecture 1
Java- language Lecture 1Java- language Lecture 1
Java- language Lecture 1
Hatem Abd El-Salam
 
introduction to embedded systems part 2
introduction to embedded systems part 2introduction to embedded systems part 2
introduction to embedded systems part 2
Hatem Abd El-Salam
 
introduction to embedded systems part 1
introduction to embedded systems part 1introduction to embedded systems part 1
introduction to embedded systems part 1
Hatem Abd El-Salam
 
C- language Lecture 6
C- language Lecture 6C- language Lecture 6
C- language Lecture 6
Hatem Abd El-Salam
 
C- language Lecture 3
C- language Lecture 3C- language Lecture 3
C- language Lecture 3
Hatem Abd El-Salam
 
C- Language Lecture 2
C- Language Lecture 2C- Language Lecture 2
C- Language Lecture 2
Hatem Abd El-Salam
 
C-language Lecture 1
C-language Lecture 1C-language Lecture 1
C-language Lecture 1
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

How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 

Recently uploaded (20)

How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 

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.