SlideShare a Scribd company logo
 A collection of one or more variables,
typically of different types, grouped together
under a single name for convenient handling
 Structure is a collection of variables of
different types under a single name.
 Known as struct in C and C++
1
 Keyword struct is used for creating a
structure.
 Syntax of structure
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
2
 We can create the structure for a person as
mentioned below as:
struct person
{
char name[50];
int citNo;
float salary;
};
This declaration above creates the derived data
type struct person.
3
When a structure is defined, it creates a user-
defined type but, no storage or memory is
allocated.
 For the above structure of a person, variable can
be declared as:
struct person
{
char name[50];
int citNo;
float salary;
};
4
int main()
{
struct person person1, person2, person3[20];
return 0;
}
5
Another way of creating a structure variable is:
struct person
{
char name[50];
int citNo;
float salary;
} person1, person2, person3[20];
6
There are two types of operators used for
accessing members of a structure.
1. Member operator(.)
2. Structure pointer operator(->)
Any member of a structure can be accessed as:
structure_variable_name.member_name
7
Suppose, we want to access salary for
variable person2.
Then, it can be accessed as: person2.salary
8
 Let
struct motor p;
struct motor q[10];
 Then
p.volts — is the voltage
p.amps — is the amperage
p.phases — is the number of phases
p.rpm — is the rotational speed
q[i].volts — is the voltage of the ith motor
q[i].rpm — is the speed of the ith motor
9
Like Java!
Writing struct structure_name variable_name; to declare a
structure variable isn't intuitive as to what it signifies,
and takes some considerable amount of development
time.
So, developers generally use typedef to name the
structure as a whole. For example:
typedef struct complex
{ int imag;
float real;
} comp;
int main()
{
comp comp1, comp2;
}
10
Structures can be nested within other
structures in C programming.
struct complex
{ int imag_value;
float real_value;
};
struct number { struct complex comp; int real; }
num1, num2;
11
Suppose, you want to access imag_value
for num2 structure variable then, following
structure member is used.
num2.comp.imag_value
12
 Copy/assign
struct motor p, q;
p = q;
 Get address
struct motor p;
struct motor *s
s = &p;
 Access members
p.volts;
s -> amps;
13
 Remember:–
 Passing an argument by value is an instance of copying or
assignment
 Passing a return value from a function to the caller is an
instance of copying or assignment
 E.g,:–
struct motor f(struct motor g) {
struct motor h = g;
...;
return h;
}
14
 K & R say (p. 131)
 “If a large structure is to be passed to a function, it is
generally more efficient to pass a pointer than to copy the
whole structure”
 I disagree:–
 Copying is very fast on modern computers
 Creating an object with malloc() and assigning a pointer
is not as fast
 Esp. if you want the object passed or returned by value
 In real life situations, it is a judgment call
15
 Let struct motor {
float volts;
float amps;
int phases;
float rpm;
}; //struct motor
 Then
struct motor m = {208, 20, 3, 1800};
initializes the struct
 See also p. 133 of K&R for initializing arrays of
structs
16
 Open-ended data structures
 E.g., structures that may grow during processing
 Avoids the need for realloc() and a lot of
copying
 Self-referential data structures
 Lists, trees, etc.
17
struct item {
char *s;
struct item *next;
}
 I.e., an item can point to another item
 … which can point to another item
 … which can point to yet another item
 … etc.
Thereby forming a list of items
18
 The following is legal:–
/* in a .c or .h file */
struct item;
struct item *p, *q;
… /* In another file */
struct item {
int member1;
float member2;
struct item *member3;
};
19
Called an opaque type!
Program can use pointers to items
but cannot see into items.
Cannot define any items, cannot
malloc any items, etc.
Implementer of item can change
the definition without forcing
users of pointers to change their
code!
 The following is not legal:–
struct motor {
float volts;
float amps;
float rpm;
unsigned int phases;
}; //struct motor
motor m;
motor *p;
20
You must write
struct motor m;
struct motor *p;
 Definition:– a typedef is a way of renaming a
type
 See §6.7
 E.g.,
typedef struct motor Motor;
Motor m, n;
Motor *p, r[25];
Motor function(const Motor m; …);
21
 typedef may be used to rename any type
 Convenience in naming
 Clarifies purpose of the type
 Cleaner, more readable code
 Portability across platforms
 E.g.,
 typedef char *String;
 E.g.,
 typedef int size_t;
 typedef long int32;
 typedef long long int64;
22
 typedef may be used to rename any type
 Convenience in naming
 Clarifies purpose of the type
 Cleaner, more readable code
 Portability across platforms
 E.g.,
 typedef char *String;
 E.g.,
 typedef int size_t;
 typedef long int32;
 typedef long long int64;
23
 The following is legal:–
/* in a .c or .h file */
typedef struct _item Item;
Item *p, *q;
… /* In another file */
struct _item {
char *info;
Item *nextItem;
};
24
25
 A union is like a struct, but only one of its
members is stored, not all
▪ I.e., a single variable may hold different types at different times
▪ Storage is enough to hold largest member
▪ Members are overlaid on top of each other
 E.g.,
union {
int ival;
float fval;
char *sval;
} u;
26
 It is programmer’s responsibility to keep track of
which type is stored in a union at any given time!
 E.g., (p. 148)
struct taggedItem {
enum {iType, fType, cType} tag;
union {
int ival;
float fval;
char *sval;
} u;
};
27
 It is programmer’s responsibility to keep track of
which type is stored in a union at any given time!
 E.g., (p. 148)
struct taggedItem {
enum {iType, fType, cType} tag;
union {
int ival;
float fval;
char *sval;
} u;
};
28
Members of struct are:–
enum tag;
union u;
Value of tag says which
member of u to use
 unions are used much less frequently than
structs — mostly
▪ in the inner details of operating system
▪ in device drivers
▪ in embedded systems where you have to access
registers defined by the hardware
29
30

More Related Content

What's hot

functions of C++
functions of C++functions of C++
functions of C++
tarandeep_kaur
 
Problem Solving Aspect of Swapping Two Integers using a Temporary Variable
Problem Solving Aspect of Swapping Two Integers using a Temporary VariableProblem Solving Aspect of Swapping Two Integers using a Temporary Variable
Problem Solving Aspect of Swapping Two Integers using a Temporary Variable
Saravana Priya
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
CGC Technical campus,Mohali
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 
C Pointers
C PointersC Pointers
C Pointers
omukhtar
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
lavanya marichamy
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
Learnbay Datascience
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
madan reddy
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
Rabin BK
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 

What's hot (20)

functions of C++
functions of C++functions of C++
functions of C++
 
Problem Solving Aspect of Swapping Two Integers using a Temporary Variable
Problem Solving Aspect of Swapping Two Integers using a Temporary VariableProblem Solving Aspect of Swapping Two Integers using a Temporary Variable
Problem Solving Aspect of Swapping Two Integers using a Temporary Variable
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
This pointer
This pointerThis pointer
This pointer
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
C Pointers
C PointersC Pointers
C Pointers
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Functions in c
Functions in cFunctions in c
Functions in c
 

Similar to Structure

C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
HimanshuSharma997566
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
shaibal sharif
 
Structures
StructuresStructures
Structures
arshpreetkaur07
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)Asfand Hassan
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
Ram Sagar Mourya
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
ansariparveen06
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
ParinayWadhwa
 
structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
AbhimanyuKumarYadav3
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
sudhakargeruganti
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
Ram Sagar Mourya
 
structures and unions in 'C'
structures and unions in 'C'structures and unions in 'C'
structures and unions in 'C'
illpa
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
Krishna Nanda
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and Typedef
Acad
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
thenmozhip8
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
C structure and union
C structure and unionC structure and union
C structure and union
Thesis Scientist Private Limited
 
Str
StrStr
Str
Acad
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 

Similar to Structure (20)

C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
Structures
StructuresStructures
Structures
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
 
structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
structures and unions in 'C'
structures and unions in 'C'structures and unions in 'C'
structures and unions in 'C'
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and Typedef
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
C structure and union
C structure and unionC structure and union
C structure and union
 
Structure
StructureStructure
Structure
 
Str
StrStr
Str
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 

More from Rokonuzzaman Rony

Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
Rokonuzzaman Rony
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
Rokonuzzaman Rony
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
Rokonuzzaman Rony
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
Rokonuzzaman Rony
 
Humanitarian task and its importance
Humanitarian task and its importanceHumanitarian task and its importance
Humanitarian task and its importance
Rokonuzzaman Rony
 
Pointers
 Pointers Pointers
Loops
LoopsLoops
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Rokonuzzaman Rony
 
Array
ArrayArray
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
Rokonuzzaman Rony
 
C Programming language
C Programming languageC Programming language
C Programming language
Rokonuzzaman Rony
 
User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
 

More from Rokonuzzaman Rony (20)

Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
 
Pointer
PointerPointer
Pointer
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Humanitarian task and its importance
Humanitarian task and its importanceHumanitarian task and its importance
Humanitarian task and its importance
 
Pointers
 Pointers Pointers
Pointers
 
Loops
LoopsLoops
Loops
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Array
ArrayArray
Array
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 
C Programming language
C Programming languageC Programming language
C Programming language
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Numerical Method 2
Numerical Method 2Numerical Method 2
Numerical Method 2
 
Numerical Method
Numerical Method Numerical Method
Numerical Method
 
Data structures
Data structuresData structures
Data structures
 
Data structures
Data structures Data structures
Data structures
 
Data structures
Data structures Data structures
Data structures
 

Recently uploaded

Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 

Recently uploaded (20)

Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 

Structure

  • 1.  A collection of one or more variables, typically of different types, grouped together under a single name for convenient handling  Structure is a collection of variables of different types under a single name.  Known as struct in C and C++ 1
  • 2.  Keyword struct is used for creating a structure.  Syntax of structure struct structure_name { data_type member1; data_type member2; . . data_type memeber; }; 2
  • 3.  We can create the structure for a person as mentioned below as: struct person { char name[50]; int citNo; float salary; }; This declaration above creates the derived data type struct person. 3
  • 4. When a structure is defined, it creates a user- defined type but, no storage or memory is allocated.  For the above structure of a person, variable can be declared as: struct person { char name[50]; int citNo; float salary; }; 4
  • 5. int main() { struct person person1, person2, person3[20]; return 0; } 5
  • 6. Another way of creating a structure variable is: struct person { char name[50]; int citNo; float salary; } person1, person2, person3[20]; 6
  • 7. There are two types of operators used for accessing members of a structure. 1. Member operator(.) 2. Structure pointer operator(->) Any member of a structure can be accessed as: structure_variable_name.member_name 7
  • 8. Suppose, we want to access salary for variable person2. Then, it can be accessed as: person2.salary 8
  • 9.  Let struct motor p; struct motor q[10];  Then p.volts — is the voltage p.amps — is the amperage p.phases — is the number of phases p.rpm — is the rotational speed q[i].volts — is the voltage of the ith motor q[i].rpm — is the speed of the ith motor 9 Like Java!
  • 10. Writing struct structure_name variable_name; to declare a structure variable isn't intuitive as to what it signifies, and takes some considerable amount of development time. So, developers generally use typedef to name the structure as a whole. For example: typedef struct complex { int imag; float real; } comp; int main() { comp comp1, comp2; } 10
  • 11. Structures can be nested within other structures in C programming. struct complex { int imag_value; float real_value; }; struct number { struct complex comp; int real; } num1, num2; 11
  • 12. Suppose, you want to access imag_value for num2 structure variable then, following structure member is used. num2.comp.imag_value 12
  • 13.  Copy/assign struct motor p, q; p = q;  Get address struct motor p; struct motor *s s = &p;  Access members p.volts; s -> amps; 13
  • 14.  Remember:–  Passing an argument by value is an instance of copying or assignment  Passing a return value from a function to the caller is an instance of copying or assignment  E.g,:– struct motor f(struct motor g) { struct motor h = g; ...; return h; } 14
  • 15.  K & R say (p. 131)  “If a large structure is to be passed to a function, it is generally more efficient to pass a pointer than to copy the whole structure”  I disagree:–  Copying is very fast on modern computers  Creating an object with malloc() and assigning a pointer is not as fast  Esp. if you want the object passed or returned by value  In real life situations, it is a judgment call 15
  • 16.  Let struct motor { float volts; float amps; int phases; float rpm; }; //struct motor  Then struct motor m = {208, 20, 3, 1800}; initializes the struct  See also p. 133 of K&R for initializing arrays of structs 16
  • 17.  Open-ended data structures  E.g., structures that may grow during processing  Avoids the need for realloc() and a lot of copying  Self-referential data structures  Lists, trees, etc. 17
  • 18. struct item { char *s; struct item *next; }  I.e., an item can point to another item  … which can point to another item  … which can point to yet another item  … etc. Thereby forming a list of items 18
  • 19.  The following is legal:– /* in a .c or .h file */ struct item; struct item *p, *q; … /* In another file */ struct item { int member1; float member2; struct item *member3; }; 19 Called an opaque type! Program can use pointers to items but cannot see into items. Cannot define any items, cannot malloc any items, etc. Implementer of item can change the definition without forcing users of pointers to change their code!
  • 20.  The following is not legal:– struct motor { float volts; float amps; float rpm; unsigned int phases; }; //struct motor motor m; motor *p; 20 You must write struct motor m; struct motor *p;
  • 21.  Definition:– a typedef is a way of renaming a type  See §6.7  E.g., typedef struct motor Motor; Motor m, n; Motor *p, r[25]; Motor function(const Motor m; …); 21
  • 22.  typedef may be used to rename any type  Convenience in naming  Clarifies purpose of the type  Cleaner, more readable code  Portability across platforms  E.g.,  typedef char *String;  E.g.,  typedef int size_t;  typedef long int32;  typedef long long int64; 22
  • 23.  typedef may be used to rename any type  Convenience in naming  Clarifies purpose of the type  Cleaner, more readable code  Portability across platforms  E.g.,  typedef char *String;  E.g.,  typedef int size_t;  typedef long int32;  typedef long long int64; 23
  • 24.  The following is legal:– /* in a .c or .h file */ typedef struct _item Item; Item *p, *q; … /* In another file */ struct _item { char *info; Item *nextItem; }; 24
  • 25. 25
  • 26.  A union is like a struct, but only one of its members is stored, not all ▪ I.e., a single variable may hold different types at different times ▪ Storage is enough to hold largest member ▪ Members are overlaid on top of each other  E.g., union { int ival; float fval; char *sval; } u; 26
  • 27.  It is programmer’s responsibility to keep track of which type is stored in a union at any given time!  E.g., (p. 148) struct taggedItem { enum {iType, fType, cType} tag; union { int ival; float fval; char *sval; } u; }; 27
  • 28.  It is programmer’s responsibility to keep track of which type is stored in a union at any given time!  E.g., (p. 148) struct taggedItem { enum {iType, fType, cType} tag; union { int ival; float fval; char *sval; } u; }; 28 Members of struct are:– enum tag; union u; Value of tag says which member of u to use
  • 29.  unions are used much less frequently than structs — mostly ▪ in the inner details of operating system ▪ in device drivers ▪ in embedded systems where you have to access registers defined by the hardware 29
  • 30. 30