SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados (20)

Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Introduction to database
Introduction to databaseIntroduction to database
Introduction to database
 
Databases: Normalisation
Databases: NormalisationDatabases: Normalisation
Databases: Normalisation
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
MySQL Functions
MySQL FunctionsMySQL Functions
MySQL Functions
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
trigger dbms
trigger dbmstrigger dbms
trigger dbms
 
User defined Function in SQL
User defined Function in SQLUser defined Function in SQL
User defined Function in SQL
 
String function in my sql
String function in my sqlString function in my sql
String function in my sql
 
Data Structures & Algorithm design using C
Data Structures & Algorithm design using C Data Structures & Algorithm design using C
Data Structures & Algorithm design using C
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAINFP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAIN
 
advanced sql(database)
advanced sql(database)advanced sql(database)
advanced sql(database)
 
List in python
List in pythonList in python
List in python
 
ORDBMS
ORDBMSORDBMS
ORDBMS
 
Linked List
Linked ListLinked List
Linked List
 

Semelhante a CP02-Structure and Union.pptx

Semelhante a 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
 

Mais de 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
 
CP03-Data Structures.pptx
CP03-Data Structures.pptxCP03-Data Structures.pptx
CP03-Data Structures.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
 

Mais de Pankaj Debbarma (17)

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
 
CP03-Data Structures.pptx
CP03-Data Structures.pptxCP03-Data Structures.pptx
CP03-Data Structures.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
 
CP01.pptx
CP01.pptxCP01.pptx
CP01.pptx
 
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
 

Último

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 

Último (20)

DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 

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