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

PPT
C++ Arrays
PDF
Managing I/O in c++
PPTX
Method overloading
PPTX
Pointer in C++
PDF
Function overloading ppt
PPTX
Tokens in C++
PPTX
Polymorphism In c++
C++ Arrays
Managing I/O in c++
Method overloading
Pointer in C++
Function overloading ppt
Tokens in C++
Polymorphism In c++

What's hot (20)

PPTX
Standard Template Library
PPT
Circular linked list
PPTX
class and objects
PPTX
Constants in java
PPT
One Dimensional Array
PPTX
Constructor and Types of Constructors
PPTX
Array in c#
PPT
Abstract class in java
PPTX
Typedef
PPTX
Type casting
PPTX
Union in c language
PPT
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Data types in c++
PPTX
Data structure & its types
PPTX
serializability in dbms
PPTX
Linked list
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
Abstract Data Types
PPTX
Arrays in c
Standard Template Library
Circular linked list
class and objects
Constants in java
One Dimensional Array
Constructor and Types of Constructors
Array in c#
Abstract class in java
Typedef
Type casting
Union in c language
Array Introduction One-dimensional array Multidimensional array
Data types in c++
Data structure & its types
serializability in dbms
Linked list
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Abstract Data Types
Arrays in c
Ad

Similar to Structure (20)

PPTX
Chapter 2 part II array and structure.pptx
PDF
C++_notes.pdf
PPTX
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
PDF
Unit 4 qba
PPTX
C programing -Structure
PDF
Structures
PPT
Introduction-to-structures-using-C-programming.ppt
PPTX
CC213-Week08-User-Defined Data Types.pptx
PPT
Structures and Unions in C-Language with Examples.ppt
PPT
Oop lec 3(structures)
PPT
C Structures & Unions
PPT
structures_v1.ppt
PPT
structures_v1.ppt
PPT
C Language_PPS_3110003_unit 8ClassPPT.ppt
PDF
structure1.pdf
PDF
Object Oriented Programming (OOP) using C++ - Lecture 1
PDF
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
PPT
C Structures And Unions
PPT
structures and unions in 'C'
Chapter 2 part II array and structure.pptx
C++_notes.pdf
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
Unit 4 qba
C programing -Structure
Structures
Introduction-to-structures-using-C-programming.ppt
CC213-Week08-User-Defined Data Types.pptx
Structures and Unions in C-Language with Examples.ppt
Oop lec 3(structures)
C Structures & Unions
structures_v1.ppt
structures_v1.ppt
C Language_PPS_3110003_unit 8ClassPPT.ppt
structure1.pdf
Object Oriented Programming (OOP) using C++ - Lecture 1
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
C Structures And Unions
structures and unions in 'C'
Ad

More from Rokonuzzaman Rony (20)

PPTX
Course outline for c programming
PPTX
PPTX
Operator Overloading & Type Conversions
PPTX
Constructors & Destructors
PPTX
Classes and objects in c++
PPTX
Functions in c++
PPTX
Object Oriented Programming with C++
PPTX
Humanitarian task and its importance
PPTX
PPTX
Introduction to C programming
PPTX
Constants, Variables, and Data Types
PPTX
C Programming language
PPTX
User defined functions
PPTX
Numerical Method 2
PPT
Numerical Method
PPTX
Data structures
PPT
Data structures
PPTX
Data structures
Course outline for c programming
Operator Overloading & Type Conversions
Constructors & Destructors
Classes and objects in c++
Functions in c++
Object Oriented Programming with C++
Humanitarian task and its importance
Introduction to C programming
Constants, Variables, and Data Types
C Programming language
User defined functions
Numerical Method 2
Numerical Method
Data structures
Data structures
Data structures

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
master seminar digital applications in india
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Cell Structure & Organelles in detailed.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
RMMM.pdf make it easy to upload and study
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Basic Mud Logging Guide for educational purpose
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
master seminar digital applications in india
O7-L3 Supply Chain Operations - ICLT Program
VCE English Exam - Section C Student Revision Booklet
human mycosis Human fungal infections are called human mycosis..pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pharma ospi slides which help in ospi learning
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Structure & Organelles in detailed.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

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