SlideShare a Scribd company logo
1 of 18
Computer Programming
ES 409
Pankaj Debbarma, Assistant Professor
Deptt. of Computer Science & Engg.,
TIT, Narsingarh
Structure Initialization
main()
{
struct
{
int weight;
float height;
}
student = (60, 180.75);
. . . . .
. . . . .
}
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
2
A structure variable
can be initialized at
compile time
Structure Initialization
main()
{
struct
{
int weight;
float height;
}
student = (60, 180.75);
. . . . .
. . . . .
}
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
3
This assigns 60 to
student.weight and
180.75 to
student.height
Structure Initialization
main()
{
struct st_record
{
int weight;
float height;
};
struct st_record student1 = (60, 180.75);
struct st_record student2 = (53, 170.60);
. . . . .
. . . . .
}
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
4
Two structure
variables can be also
initialized
Structure Initialization
struct st_record
{
int weight;
float height;
} student1 = (60, 180.75);
main()
{
struct st_record student2 = (53, 170.60);
. . . . .
. . . . .
}
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
5
Another method is to
initialize a structure
variable outside the
function
Structure Initialization
1. We cannot initialize individual members inside
the structure template.
2. The order of values enclosed in braces must
match the order of members in the structure
definition.
3. It is permitted to have a partial initialization.
4. The uninitialized members will be assigned
default values as follows:
• Zero for integer and floating point numbers
• ‘0’ for characters and strings
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
6
Structure Initialization
• Two variables of the same structure type can
be copied the same way as ordinary variables.
person1 = person2;
person2 = person1;
• C does not permit any logical operations on
structure variables.
person1 == person2
person1 != person2
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
7
Accessing members
Consider the following structure:
typedef struct
{
int x;
int y;
} VECTOR;
VECTOR v, *ptr;
ptr = &v;
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
8
Accessing members
The identifier ptr is known as pointer that has
been assigned the address of the structure
variable v. Now the members can be accessed in
the following three ways:
• using dot notation : v.x
• using indirection notation : (*ptr).x
• using selection notation : ptr  x
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
9
Pointers and Structures
struct inventory
{
char name[30];
int number;
float price;
} product[2], *ptr;
This statement declares product as an array of two
elements, each of the type struct inventory and ptr as a
pointer to data objects of the type struct inventory.
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
10
Pointers and Structures
The assignment
ptr = product;
would assign the address of the zeroth element
of product to ptr. That is, the pointer ptr will
now point to product[0]. Its member can be
accessed using
ptr  name
ptr  number
ptr  price
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
11
Structures and Functions
There are three methods by which values of a
structure can be transferred from one function
to another.
1. Pass each member of the structure as an
actual argument of the function call.
2. Passing a copy of the entire structure to the
called function.
3. The address location of the structure is
passed to the called function.
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
12
Unions
union item
{
int m;
float x;
char c;
} code;
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
13
Unions
During accessing, we should make sure that we
are accessing the member whose value is
currently stored. For example, the statements
such as
code.m = 379;
code.x = 7859.36
printf(“%d”, code.m)
would produce erroneous output
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
14
Unions
Unions may be initialized when the variable is
declared. But, unlike structures, it can be
initialized only with a value of the same type as
the first union member.
union item abc = {100};
is valid but the declaration
union item abc = {10.75};
is invalid
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
15
Size of Structures
The expression
sizeof(struct x)
will evaluate the number of bytes required to hold
all the members of the structure x. If y is a simple
structure variable of type struct x, then
sizeof(y);
would also give the same answer. However, if y is an
array variable of type struct x, then it would give
the total number of bytes the array y requires.
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
16
Size of Structures
This kind of information would be useful to
determine the number of records in a database.
For example, the expression
sizeof(y) / sizeof(x)
would give the number of elements in the array
y.
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
17
CP02-Structure and Union.pptx

More Related Content

What's hot

Programação Estruturada 2 - Curso Completo
Programação Estruturada 2 - Curso CompletoProgramação Estruturada 2 - Curso Completo
Programação Estruturada 2 - Curso Completothomasdacosta
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In CFazila Sadia
 
Design de interfaces gráficas
Design de interfaces gráficasDesign de interfaces gráficas
Design de interfaces gráficasUTFPR
 
DFD ระบบจองรีสอร์ทออนไลน์
DFD ระบบจองรีสอร์ทออนไลน์DFD ระบบจองรีสอร์ทออนไลน์
DFD ระบบจองรีสอร์ทออนไลน์rubtumproject.com
 

What's hot (8)

Class diagram, use case and sequence diagram
Class diagram, use case and sequence diagramClass diagram, use case and sequence diagram
Class diagram, use case and sequence diagram
 
Programação Estruturada 2 - Curso Completo
Programação Estruturada 2 - Curso CompletoProgramação Estruturada 2 - Curso Completo
Programação Estruturada 2 - Curso Completo
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
 
Python standard data types
Python standard data typesPython standard data types
Python standard data types
 
Chapter 3.pptx
Chapter 3.pptxChapter 3.pptx
Chapter 3.pptx
 
Estruturas de dados
Estruturas de dadosEstruturas de dados
Estruturas de dados
 
Design de interfaces gráficas
Design de interfaces gráficasDesign de interfaces gráficas
Design de interfaces gráficas
 
DFD ระบบจองรีสอร์ทออนไลน์
DFD ระบบจองรีสอร์ทออนไลน์DFD ระบบจองรีสอร์ทออนไลน์
DFD ระบบจองรีสอร์ทออนไลน์
 

Similar to CP02-Structure and Union.pptx

Similar to CP02-Structure and Union.pptx (20)

structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
 
SimpleArray between Python and C++
SimpleArray between Python and C++SimpleArray between Python and C++
SimpleArray between Python and C++
 
C programming structures & union
C programming structures & unionC programming structures & union
C programming structures & union
 
structures.ppt
structures.pptstructures.ppt
structures.ppt
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.ppt
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
 
Structure
StructureStructure
Structure
 
Lecture5
Lecture5Lecture5
Lecture5
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And Enums
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 
Lk module3
Lk module3Lk module3
Lk module3
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)
 
Unit 3
Unit 3 Unit 3
Unit 3
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure Programming
 
Overloading
OverloadingOverloading
Overloading
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
 

More from Pankaj Debbarma

OS-01 Virtual Memory.pptx
OS-01 Virtual Memory.pptxOS-01 Virtual Memory.pptx
OS-01 Virtual Memory.pptxPankaj Debbarma
 
AI-06 Search Techniques - Informed.pptx
AI-06 Search Techniques - Informed.pptxAI-06 Search Techniques - Informed.pptx
AI-06 Search Techniques - Informed.pptxPankaj Debbarma
 
AI-05 Search Algorithms.pptx
AI-05 Search Algorithms.pptxAI-05 Search Algorithms.pptx
AI-05 Search Algorithms.pptxPankaj Debbarma
 
AI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptxAI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptxPankaj Debbarma
 
AI-03 Problems State Space.pptx
AI-03 Problems State Space.pptxAI-03 Problems State Space.pptx
AI-03 Problems State Space.pptxPankaj Debbarma
 
Computer Graphics & Visualization - 06
Computer Graphics & Visualization - 06Computer Graphics & Visualization - 06
Computer Graphics & Visualization - 06Pankaj Debbarma
 
NETWORK LAYER - Logical Addressing
NETWORK LAYER - Logical AddressingNETWORK LAYER - Logical Addressing
NETWORK LAYER - Logical AddressingPankaj Debbarma
 
TRANSPORT LAYER - Process-to-Process Delivery: UDP, TCP and SCTP
TRANSPORT LAYER - Process-to-Process Delivery: UDP, TCP and SCTPTRANSPORT LAYER - Process-to-Process Delivery: UDP, TCP and SCTP
TRANSPORT LAYER - Process-to-Process Delivery: UDP, TCP and SCTPPankaj Debbarma
 

More from Pankaj Debbarma (15)

AI-09 Logic in AI
AI-09 Logic in AIAI-09 Logic in AI
AI-09 Logic in AI
 
OS-02 Segmentation.pptx
OS-02 Segmentation.pptxOS-02 Segmentation.pptx
OS-02 Segmentation.pptx
 
AI-08 Game Playing.pptx
AI-08 Game Playing.pptxAI-08 Game Playing.pptx
AI-08 Game Playing.pptx
 
OS-01 Virtual Memory.pptx
OS-01 Virtual Memory.pptxOS-01 Virtual Memory.pptx
OS-01 Virtual Memory.pptx
 
AI-06 Search Techniques - Informed.pptx
AI-06 Search Techniques - Informed.pptxAI-06 Search Techniques - Informed.pptx
AI-06 Search Techniques - Informed.pptx
 
AI-05 Search Algorithms.pptx
AI-05 Search Algorithms.pptxAI-05 Search Algorithms.pptx
AI-05 Search Algorithms.pptx
 
AI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptxAI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptx
 
AI-03 Problems State Space.pptx
AI-03 Problems State Space.pptxAI-03 Problems State Space.pptx
AI-03 Problems State Space.pptx
 
Intelligent Agents
Intelligent AgentsIntelligent Agents
Intelligent Agents
 
Computer Graphics & Visualization - 06
Computer Graphics & Visualization - 06Computer Graphics & Visualization - 06
Computer Graphics & Visualization - 06
 
HTTP and Email
HTTP and EmailHTTP and Email
HTTP and Email
 
Ppt World Wide Web
Ppt World Wide WebPpt World Wide Web
Ppt World Wide Web
 
Ppt congestion control
Ppt congestion controlPpt congestion control
Ppt congestion control
 
NETWORK LAYER - Logical Addressing
NETWORK LAYER - Logical AddressingNETWORK LAYER - Logical Addressing
NETWORK LAYER - Logical Addressing
 
TRANSPORT LAYER - Process-to-Process Delivery: UDP, TCP and SCTP
TRANSPORT LAYER - Process-to-Process Delivery: UDP, TCP and SCTPTRANSPORT LAYER - Process-to-Process Delivery: UDP, TCP and SCTP
TRANSPORT LAYER - Process-to-Process Delivery: UDP, TCP and SCTP
 

Recently uploaded

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projectssmsksolar
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086anil_gaur
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf203318pmpc
 

Recently uploaded (20)

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 

CP02-Structure and Union.pptx

  • 1. Computer Programming ES 409 Pankaj Debbarma, Assistant Professor Deptt. of Computer Science & Engg., TIT, Narsingarh
  • 2. Structure Initialization main() { struct { int weight; float height; } student = (60, 180.75); . . . . . . . . . . } Computer Programming – Pankaj Debbarma, TIT, Narsingarh 2 A structure variable can be initialized at compile time
  • 3. Structure Initialization main() { struct { int weight; float height; } student = (60, 180.75); . . . . . . . . . . } Computer Programming – Pankaj Debbarma, TIT, Narsingarh 3 This assigns 60 to student.weight and 180.75 to student.height
  • 4. Structure Initialization main() { struct st_record { int weight; float height; }; struct st_record student1 = (60, 180.75); struct st_record student2 = (53, 170.60); . . . . . . . . . . } Computer Programming – Pankaj Debbarma, TIT, Narsingarh 4 Two structure variables can be also initialized
  • 5. Structure Initialization struct st_record { int weight; float height; } student1 = (60, 180.75); main() { struct st_record student2 = (53, 170.60); . . . . . . . . . . } Computer Programming – Pankaj Debbarma, TIT, Narsingarh 5 Another method is to initialize a structure variable outside the function
  • 6. Structure Initialization 1. We cannot initialize individual members inside the structure template. 2. The order of values enclosed in braces must match the order of members in the structure definition. 3. It is permitted to have a partial initialization. 4. The uninitialized members will be assigned default values as follows: • Zero for integer and floating point numbers • ‘0’ for characters and strings Computer Programming – Pankaj Debbarma, TIT, Narsingarh 6
  • 7. Structure Initialization • Two variables of the same structure type can be copied the same way as ordinary variables. person1 = person2; person2 = person1; • C does not permit any logical operations on structure variables. person1 == person2 person1 != person2 Computer Programming – Pankaj Debbarma, TIT, Narsingarh 7
  • 8. Accessing members Consider the following structure: typedef struct { int x; int y; } VECTOR; VECTOR v, *ptr; ptr = &v; Computer Programming – Pankaj Debbarma, TIT, Narsingarh 8
  • 9. Accessing members The identifier ptr is known as pointer that has been assigned the address of the structure variable v. Now the members can be accessed in the following three ways: • using dot notation : v.x • using indirection notation : (*ptr).x • using selection notation : ptr  x Computer Programming – Pankaj Debbarma, TIT, Narsingarh 9
  • 10. Pointers and Structures struct inventory { char name[30]; int number; float price; } product[2], *ptr; This statement declares product as an array of two elements, each of the type struct inventory and ptr as a pointer to data objects of the type struct inventory. Computer Programming – Pankaj Debbarma, TIT, Narsingarh 10
  • 11. Pointers and Structures The assignment ptr = product; would assign the address of the zeroth element of product to ptr. That is, the pointer ptr will now point to product[0]. Its member can be accessed using ptr  name ptr  number ptr  price Computer Programming – Pankaj Debbarma, TIT, Narsingarh 11
  • 12. Structures and Functions There are three methods by which values of a structure can be transferred from one function to another. 1. Pass each member of the structure as an actual argument of the function call. 2. Passing a copy of the entire structure to the called function. 3. The address location of the structure is passed to the called function. Computer Programming – Pankaj Debbarma, TIT, Narsingarh 12
  • 13. Unions union item { int m; float x; char c; } code; Computer Programming – Pankaj Debbarma, TIT, Narsingarh 13
  • 14. Unions During accessing, we should make sure that we are accessing the member whose value is currently stored. For example, the statements such as code.m = 379; code.x = 7859.36 printf(“%d”, code.m) would produce erroneous output Computer Programming – Pankaj Debbarma, TIT, Narsingarh 14
  • 15. Unions Unions may be initialized when the variable is declared. But, unlike structures, it can be initialized only with a value of the same type as the first union member. union item abc = {100}; is valid but the declaration union item abc = {10.75}; is invalid Computer Programming – Pankaj Debbarma, TIT, Narsingarh 15
  • 16. Size of Structures The expression sizeof(struct x) will evaluate the number of bytes required to hold all the members of the structure x. If y is a simple structure variable of type struct x, then sizeof(y); would also give the same answer. However, if y is an array variable of type struct x, then it would give the total number of bytes the array y requires. Computer Programming – Pankaj Debbarma, TIT, Narsingarh 16
  • 17. Size of Structures This kind of information would be useful to determine the number of records in a database. For example, the expression sizeof(y) / sizeof(x) would give the number of elements in the array y. Computer Programming – Pankaj Debbarma, TIT, Narsingarh 17