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

Operator Overloading & Function Overloading
Operator Overloading & Function OverloadingOperator Overloading & Function Overloading
Operator Overloading & Function Overloading
Meghaj Mallick
 
Pointer in c
Pointer in cPointer in c
Pointer in c
lavanya marichamy
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Data Types In C
Data Types In CData Types In C
Data Types In C
Simplilearn
 
Chapter 4 strings
Chapter 4 stringsChapter 4 strings
Chapter 4 strings
Chv Raghavendran
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
Gem WeBlog
 
Functional dependencies in Database Management System
Functional dependencies in Database Management SystemFunctional dependencies in Database Management System
Functional dependencies in Database Management System
Kevin Jadiya
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
Mauryasuraj98
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
Sreedhar Chowdam
 
Functions in C
Functions in CFunctions in C
Functions in C
Shobhit Upadhyay
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Mansi Tyagi
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
Dhrumil Patel
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
Abu Bakr Ramadan
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Combinational circuits
Combinational circuits Combinational circuits
Combinational circuits
DrSonali Vyas
 
Pointers
PointersPointers
Pointers
rajshreemuthiah
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
programming9
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 
Unit 3 dsa LINKED LIST
Unit 3 dsa LINKED LISTUnit 3 dsa LINKED LIST

What's hot (20)

Operator Overloading & Function Overloading
Operator Overloading & Function OverloadingOperator Overloading & Function Overloading
Operator Overloading & Function Overloading
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Data Types In C
Data Types In CData Types In C
Data Types In C
 
Chapter 4 strings
Chapter 4 stringsChapter 4 strings
Chapter 4 strings
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Functional dependencies in Database Management System
Functional dependencies in Database Management SystemFunctional dependencies in Database Management System
Functional dependencies in Database Management System
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Strings in C
Strings in CStrings in C
Strings in C
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Combinational circuits
Combinational circuits Combinational circuits
Combinational circuits
 
Pointers
PointersPointers
Pointers
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Unit 3 dsa LINKED LIST
Unit 3 dsa LINKED LISTUnit 3 dsa LINKED LIST
Unit 3 dsa LINKED LIST
 

Similar to CP02-Structure and Union.pptx

structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
AbhimanyuKumarYadav3
 
SimpleArray between Python and C++
SimpleArray between Python and C++SimpleArray between Python and C++
SimpleArray between Python and C++
Yung-Yu Chen
 
C programming structures & union
C programming structures & unionC programming structures & union
C programming structures & union
Bathshebaparimala
 
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
shivani366010
 
structures.ppt
structures.pptstructures.ppt
structures.ppt
RamyaR163211
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
TAlha MAlik
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
babuk110
 
Structure
StructureStructure
Structure
ALI RAZA
 
Lecture5
Lecture5Lecture5
Lecture5
ravifeelings
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And Enums
Bhushan Mulmule
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
skilljiolms
 
Lk module3
Lk module3Lk module3
Lk module3
Krishna Nanda
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)
Asfand Hassan
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
thenmozhip8
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure Programming
Marian Marinov
 
Overloading
OverloadingOverloading
Overloading
poonamchopra7975
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
Mehul Desai
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
Paulo Morgado
 

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
 
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
 
structures.ppt
structures.pptstructures.ppt
structures.ppt
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 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
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
 

More from Pankaj Debbarma

AI-09 Logic in AI
AI-09 Logic in AIAI-09 Logic in AI
AI-09 Logic in AI
Pankaj Debbarma
 
OS-02 Segmentation.pptx
OS-02 Segmentation.pptxOS-02 Segmentation.pptx
OS-02 Segmentation.pptx
Pankaj Debbarma
 
AI-08 Game Playing.pptx
AI-08 Game Playing.pptxAI-08 Game Playing.pptx
AI-08 Game Playing.pptx
Pankaj Debbarma
 
OS-01 Virtual Memory.pptx
OS-01 Virtual Memory.pptxOS-01 Virtual Memory.pptx
OS-01 Virtual Memory.pptx
Pankaj Debbarma
 
AI-06 Search Techniques - Informed.pptx
AI-06 Search Techniques - Informed.pptxAI-06 Search Techniques - Informed.pptx
AI-06 Search Techniques - Informed.pptx
Pankaj Debbarma
 
AI-05 Search Algorithms.pptx
AI-05 Search Algorithms.pptxAI-05 Search Algorithms.pptx
AI-05 Search Algorithms.pptx
Pankaj 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.pptx
Pankaj Debbarma
 
AI-03 Problems State Space.pptx
AI-03 Problems State Space.pptxAI-03 Problems State Space.pptx
AI-03 Problems State Space.pptx
Pankaj Debbarma
 
Intelligent Agents
Intelligent AgentsIntelligent Agents
Intelligent Agents
Pankaj Debbarma
 
Computer Graphics & Visualization - 06
Computer Graphics & Visualization - 06Computer Graphics & Visualization - 06
Computer Graphics & Visualization - 06
Pankaj Debbarma
 
HTTP and Email
HTTP and EmailHTTP and Email
HTTP and Email
Pankaj Debbarma
 
Ppt World Wide Web
Ppt World Wide WebPpt World Wide Web
Ppt World Wide Web
Pankaj Debbarma
 
Ppt congestion control
Ppt congestion controlPpt congestion control
Ppt congestion control
Pankaj Debbarma
 
NETWORK LAYER - Logical Addressing
NETWORK LAYER - Logical AddressingNETWORK LAYER - Logical Addressing
NETWORK LAYER - Logical Addressing
Pankaj 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 SCTP
Pankaj 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

KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))
shivani5543
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
Madan Karki
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Hematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood CountHematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood Count
shahdabdulbaset
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 

Recently uploaded (20)

KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Hematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood CountHematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood Count
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 

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