SlideShare a Scribd company logo
STRUCTURE
WHY STRUCTURE?
 Data structures are a feature that can be used to represent databases, especially if we consider the
possibility of building arrays of them:
 The structure tag can be any legal identifier (but not a keyword).
 The name of a structure type is called the structure tag.
 The identifiers declared inside the braces,{ }, are called member names.
 A structure type definition ends with both a brace, }, and a semicolon.
 Once a structure type definition has been given, the structure type can be used just like the
predefined types int, char, and so forth.
 A structure variable can hold values just like any other variable can hold values.
 A structure value is a collection of smaller values called member values.
 There is one member value for each member name declared in the structure definition.
 A structure definition is usually placed outside of any function definition. The structure type is then available to all the code that
follows the structure definition.
struct Structure_Tag
{
Type_1 Member_Variable_Name_1;
Type_2 Member_Variable_Name_2;
.
.
.
Type_Last Member_Variable_Name_Last;
}[variablename,anothervariableifany]; // }[variablename,anothervariableifany] is optional
 Variables of a structure type can be declared in the same way as variables of other types.
struct course{
string coursename;
string ccode;
int Ects, Chour;
};
INITIALIZATION
Struct Date{
int day, month,year;
};
 Date due_date = {12, 31, 2004};
 Date anotherDate=due_date;
 It is an error if there are more initializers than struct members. If there
 are fewer initializer values than struct members, the provided values are used
 to initialize data members, in order. Each data member without an initializer
 is initialized to a zero value of an appropriate type for the variable.
 The structure type is then available to all the code that follows the structure definition.
 Two or more structure types may use the same member names.
struct FertilizerStock
{
double quantity;
double nitrogen_content;
};
struct CropYield
{
int quantity;
double size;
};
 This coincidence of names will produce no problems. Why?
 The dot operator is used to specify a member variable of a structure variable.
 Structure_Variable_Name.Member_Variable_NameEXAMPLES
 Structure_Variable_Name.Member_Variable_Name
 EXAMPLES
struct StudentRecord
{
int student_number;
char grade;
};
int main( )
{
StudentRecord your_record;
your_record.student_number = 2001;
your_record.grade = 'A';
Some writers call the dot operator the structure member access operator although we will not
use that term.
 You must also place a semicolon after that final brace.
 A structure definition is more than a definition. It can also be used to declare structure variables.
 A function can have call-by-value parameters of a structure type and/or call by-reference parameters of a
structure type.
 A function can have call-by-value parameters of a structure type and/or call by- reference parameters of a
structure type.
 A structure type can also be the type for the value returned by a function.
 Sometimes it makes sense to have structures whose members are themselves smaller structures.
struct Date
{
int month;
int day;
int year;
};
struct PersonInfo
{
double height;//in inches
int weight;//in pounds
Date birthday;
};
PersonInfo person1;
cout << person1.birthday.year;
POINTERS TO ACCESS DATA WITHIN A STRUCTURE.
Date *d;
(*d).month=12;
D-->month=12;
Student * members = new student[100];
//contributions is a structure;
members[i].batch=2009;
Or
*(members+i)-->batch= 2009;
members[i]->batch= 2009; //Using both [] and -> doesn't work
because then you'd be dereferencing twice.
ARRAY OF STRUCTURES
 It is possible to have an array of structures. As StructureName arrayName[size];
 To access arrayName[index].memebervariable
Struct student {
. . .
. . .
} st[100];
To access members you can use st[0].member=value;
e.g.
personInfo persons[100];
persons[0].height=1.83;
persons[5].weight=75;

More Related Content

PPTX
Structures in c language
PPTX
Structures in c language
PDF
Structures in c++
PPTX
Objects and Types C#
PPTX
Structure & union
PPTX
Structure prespentation
PDF
Structures in c++
PDF
Unit 4 qba
Structures in c language
Structures in c language
Structures in c++
Objects and Types C#
Structure & union
Structure prespentation
Structures in c++
Unit 4 qba

Similar to Structure.pptx (20)

PDF
Chapter 13.1.9
PPT
SPC Unit 5
PDF
C structure and union
PPT
Introduction-to-structures-using-C-programming.ppt
PPTX
Structure in c language
PPT
structures_v1.ppt
PPT
structures_v1.ppt
PDF
Lk module4 structures
PPTX
C programing -Structure
PPT
Structures and Unions in C-Language with Examples.ppt
PPTX
User defined data types.pptx
PDF
Chapter 7 (Part I) - User Defined Datatypes.pdf
PPTX
Computer science ( Structures In C ) Ppt
PPT
Advanced c#
PPT
structures and unions in 'C'
PDF
unit 5.pdf structure pdf is here you can do this
PPT
C Structures & Unions
PPTX
Unit 1_ADC.pptx
DOCX
Str
DOC
9.structure & union
Chapter 13.1.9
SPC Unit 5
C structure and union
Introduction-to-structures-using-C-programming.ppt
Structure in c language
structures_v1.ppt
structures_v1.ppt
Lk module4 structures
C programing -Structure
Structures and Unions in C-Language with Examples.ppt
User defined data types.pptx
Chapter 7 (Part I) - User Defined Datatypes.pdf
Computer science ( Structures In C ) Ppt
Advanced c#
structures and unions in 'C'
unit 5.pdf structure pdf is here you can do this
C Structures & Unions
Unit 1_ADC.pptx
Str
9.structure & union

Recently uploaded (20)

PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
cloud_computing_Infrastucture_as_cloud_p
PPTX
A Presentation on Artificial Intelligence
PPTX
1. Introduction to Computer Programming.pptx
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
A Presentation on Touch Screen Technology
Web App vs Mobile App What Should You Build First.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Encapsulation_ Review paper, used for researhc scholars
Unlocking AI with Model Context Protocol (MCP)
Univ-Connecticut-ChatGPT-Presentaion.pdf
Heart disease approach using modified random forest and particle swarm optimi...
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Enhancing emotion recognition model for a student engagement use case through...
cloud_computing_Infrastucture_as_cloud_p
A Presentation on Artificial Intelligence
1. Introduction to Computer Programming.pptx
Tartificialntelligence_presentation.pptx
Group 1 Presentation -Planning and Decision Making .pptx
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
A Presentation on Touch Screen Technology

Structure.pptx

  • 2.  Data structures are a feature that can be used to represent databases, especially if we consider the possibility of building arrays of them:  The structure tag can be any legal identifier (but not a keyword).  The name of a structure type is called the structure tag.  The identifiers declared inside the braces,{ }, are called member names.  A structure type definition ends with both a brace, }, and a semicolon.  Once a structure type definition has been given, the structure type can be used just like the predefined types int, char, and so forth.  A structure variable can hold values just like any other variable can hold values.
  • 3.  A structure value is a collection of smaller values called member values.  There is one member value for each member name declared in the structure definition.  A structure definition is usually placed outside of any function definition. The structure type is then available to all the code that follows the structure definition. struct Structure_Tag { Type_1 Member_Variable_Name_1; Type_2 Member_Variable_Name_2; . . . Type_Last Member_Variable_Name_Last; }[variablename,anothervariableifany]; // }[variablename,anothervariableifany] is optional  Variables of a structure type can be declared in the same way as variables of other types. struct course{ string coursename; string ccode; int Ects, Chour; };
  • 4. INITIALIZATION Struct Date{ int day, month,year; };  Date due_date = {12, 31, 2004};  Date anotherDate=due_date;  It is an error if there are more initializers than struct members. If there  are fewer initializer values than struct members, the provided values are used  to initialize data members, in order. Each data member without an initializer  is initialized to a zero value of an appropriate type for the variable.
  • 5.  The structure type is then available to all the code that follows the structure definition.  Two or more structure types may use the same member names. struct FertilizerStock { double quantity; double nitrogen_content; }; struct CropYield { int quantity; double size; };  This coincidence of names will produce no problems. Why?
  • 6.  The dot operator is used to specify a member variable of a structure variable.  Structure_Variable_Name.Member_Variable_NameEXAMPLES  Structure_Variable_Name.Member_Variable_Name  EXAMPLES struct StudentRecord { int student_number; char grade; }; int main( ) { StudentRecord your_record; your_record.student_number = 2001; your_record.grade = 'A'; Some writers call the dot operator the structure member access operator although we will not use that term.
  • 7.  You must also place a semicolon after that final brace.  A structure definition is more than a definition. It can also be used to declare structure variables.  A function can have call-by-value parameters of a structure type and/or call by-reference parameters of a structure type.  A function can have call-by-value parameters of a structure type and/or call by- reference parameters of a structure type.  A structure type can also be the type for the value returned by a function.  Sometimes it makes sense to have structures whose members are themselves smaller structures.
  • 8. struct Date { int month; int day; int year; }; struct PersonInfo { double height;//in inches int weight;//in pounds Date birthday; }; PersonInfo person1; cout << person1.birthday.year; POINTERS TO ACCESS DATA WITHIN A STRUCTURE. Date *d; (*d).month=12; D-->month=12; Student * members = new student[100]; //contributions is a structure; members[i].batch=2009; Or *(members+i)-->batch= 2009; members[i]->batch= 2009; //Using both [] and -> doesn't work because then you'd be dereferencing twice.
  • 9. ARRAY OF STRUCTURES  It is possible to have an array of structures. As StructureName arrayName[size];  To access arrayName[index].memebervariable Struct student { . . . . . . } st[100]; To access members you can use st[0].member=value; e.g. personInfo persons[100]; persons[0].height=1.83; persons[5].weight=75;