SlideShare a Scribd company logo
1 of 5
Download to read offline
www.cppforschool.com
Structure
A structure is a collection of variable which can be same or different types. You
can refer to a structure as a single variable and to its parts as members of that
variable by using the dot (.) operator. The power of structures lies in the fact
that once defined, the structure name becomes a user-defined data type and
may be used the same way as other built-in data types, such as int, double,
char.
struct Student
{
int rollno, age;
char name[80];
float marks;
};
int main()
{
// declare two variables of the new type
Student s1, s3;
//accessing of data members
cin >> s1.rollno >> s1.age >> s1.name >> s1.marks;
cout << s1.rollno << s1.age << s1.name << s1.marks;
//initialization of structure variable
Student s2 = {100, 17, "Aniket", 92};
cout << s2.rollno << s2.age << s2.name << s2.marks;
//structure variable in assignment statement
s3 = s2;
cout << s3.rollno << s3.age << s3.name << s3.marks;
return 0;
}
Defining a structure
When dealing with the students in a school, many variables of different types
are needed. It may be necessary to keep track of name, age, Rollno, and
marks point for example.
struct Student
{
int rollno, age;
char name[80];
float marks;
};
Student is called the structure tag, and is your brand new data type, like int,
double or char.
rollno, name, age, and marks are structure members.
Declaring Variables of Type struct
The most efficient method of dealing with structure variables is to define the
structure globally. This tells "the whole world", namely main and any functions
in the program, that a new data type exists. To declare a structure globally,
place it BEFORE void main(). The structure variables can then be defined
locally in main, for example…
struct Student
{
int rollno, age;
char name[80];
float marks;
};
int main()
{
// declare two variables of the new type
Student s1, s3;
………
………
return 0;
}
Alternate method of declaring variables of type struct:
struct Student
{
int rollno, age;
char name[80];
float marks;
} s1, s3;
Accessing of data members
The accessing of data members is done by using the following format:
structure variable.member name
for example
cin >> s1.rollno >> s1.age >> s1.name >> s1.marks;
Initialization of structure variable
Initialization is done at the time of declaration of a variable. For example
Student s2 = {100, 17, "Aniket", 92};
Structure variable in assignment statement
s3 = s2;
The statement assigns the value of each member of s2 to the corresponding
member of s3. Note that one structure variable can be assigned to another only
when they are of the same structure type, otherwise complier will give an error.
Nested structure (Structure within structure)
It is possible to use a structure to define another structure. This is called
nesting of structure. Consider the following program
struct Day
{
int month, date, year;
};
struct Student
{
int rollno, age;
char name[80];
Day date_of_birth;
float marks;
};
Accessing Member variables of Student
To access members of date_of_birth we can write the statements as below :
Student s; // Structure variable of Student
s.date_of_birth.month = 11;
s.date_of_birth.date = 5;
s.date_of_birth.year = 1999;
typedef
It is used to define new data type for an existing data type. It provides and
alternative name for standard data type. It is used for self documenting the
code by allowing descriptive name for the standard data type.
The general format is:
typedef existing datatype new datatype
for example:
typedef float real;
Now, in a program one can use datatype real instead of float.
Therefore, the following statement is valid:
real amount;
Enumerated data type
The enum specifier defines the set of names which are stored internally as
integer constant. The first name was given the integer value 0, the second
value 1 and so on.
for example:
enum months{jan, feb, mar, apr, may} ;
It has the following features:
 It is user defined.
 It works if you know in advance a finite list of values that a data type can
take.
 The list cannot be input by the user or output on the screen.
#define preprocessor directive
The #define preprocessor allows to define symbolic names and constants e.g.
#define pi 3.14159
This statement will translate every occurrence of PI in the program to 3.14159
Macros
Macros are built on the #define preprocessor. Normally a macro would look like:
#define square(x) x*x
Its arguments substituted for replacement text, when the macro is expanded.

More Related Content

What's hot

psnreddy php-oops
psnreddy  php-oopspsnreddy  php-oops
psnreddy php-oops
satya552
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 

What's hot (20)

Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Structures
StructuresStructures
Structures
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 
psnreddy php-oops
psnreddy  php-oopspsnreddy  php-oops
psnreddy php-oops
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
Structures
StructuresStructures
Structures
 
Arrays
ArraysArrays
Arrays
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Structure in c sharp
Structure in c sharpStructure in c sharp
Structure in c sharp
 
Java 1-contd
Java 1-contdJava 1-contd
Java 1-contd
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
03 structures
03 structures03 structures
03 structures
 
Java Script Introduction
Java Script IntroductionJava Script Introduction
Java Script Introduction
 
Structures,pointers and strings in c Programming
Structures,pointers and strings in c ProgrammingStructures,pointers and strings in c Programming
Structures,pointers and strings in c Programming
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
 
C++ programming structure & union
C++ programming structure & unionC++ programming structure & union
C++ programming structure & union
 

Viewers also liked (14)

Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
 
Chapter17 oop
Chapter17 oopChapter17 oop
Chapter17 oop
 
Chapter24 operator-overloading
Chapter24 operator-overloadingChapter24 operator-overloading
Chapter24 operator-overloading
 
Chapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-filesChapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-files
 
Chapter16 pointer
Chapter16 pointerChapter16 pointer
Chapter16 pointer
 
Chapter19 constructor-and-destructor
Chapter19 constructor-and-destructorChapter19 constructor-and-destructor
Chapter19 constructor-and-destructor
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-file
 
Chapter20 class-example-program
Chapter20 class-example-programChapter20 class-example-program
Chapter20 class-example-program
 
Chapter25 inheritance-i
Chapter25 inheritance-iChapter25 inheritance-i
Chapter25 inheritance-i
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes
 

Similar to Chapter15 structure

Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
patcha535
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
mrecedu
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
Alisha Korpal
 

Similar to Chapter15 structure (20)

Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
Structures
StructuresStructures
Structures
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
C structure and union
C structure and unionC structure and union
C structure and union
 
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
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
 
Structures
StructuresStructures
Structures
 
structure.ppt
structure.pptstructure.ppt
structure.ppt
 
structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
 
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
 
Structure In C
Structure In CStructure In C
Structure In C
 
Structure and Enum in c#
Structure and Enum in c#Structure and Enum in c#
Structure and Enum in c#
 
Structure for cpu
Structure for cpuStructure for cpu
Structure for cpu
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 

More from Deepak Singh

Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
Deepak Singh
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objects
Deepak Singh
 
Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-array
Deepak Singh
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimension
Deepak Singh
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library Function
Deepak Singh
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
Deepak Singh
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak Singh
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++
Deepak Singh
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
Deepak Singh
 
Chapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptChapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory Concept
Deepak Singh
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
Deepak Singh
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
Deepak Singh
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
Deepak Singh
 

More from Deepak Singh (17)

Computer networks - CBSE New Syllabus (083) Class - XII
Computer networks - CBSE  New Syllabus (083) Class - XIIComputer networks - CBSE  New Syllabus (083) Class - XII
Computer networks - CBSE New Syllabus (083) Class - XII
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objects
 
Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-array
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimension
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library Function
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
 
Chapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptChapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory Concept
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
 

Chapter15 structure

  • 1. www.cppforschool.com Structure A structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable and to its parts as members of that variable by using the dot (.) operator. The power of structures lies in the fact that once defined, the structure name becomes a user-defined data type and may be used the same way as other built-in data types, such as int, double, char. struct Student { int rollno, age; char name[80]; float marks; }; int main() { // declare two variables of the new type Student s1, s3; //accessing of data members cin >> s1.rollno >> s1.age >> s1.name >> s1.marks; cout << s1.rollno << s1.age << s1.name << s1.marks; //initialization of structure variable Student s2 = {100, 17, "Aniket", 92}; cout << s2.rollno << s2.age << s2.name << s2.marks; //structure variable in assignment statement s3 = s2; cout << s3.rollno << s3.age << s3.name << s3.marks; return 0; }
  • 2. Defining a structure When dealing with the students in a school, many variables of different types are needed. It may be necessary to keep track of name, age, Rollno, and marks point for example. struct Student { int rollno, age; char name[80]; float marks; }; Student is called the structure tag, and is your brand new data type, like int, double or char. rollno, name, age, and marks are structure members. Declaring Variables of Type struct The most efficient method of dealing with structure variables is to define the structure globally. This tells "the whole world", namely main and any functions in the program, that a new data type exists. To declare a structure globally, place it BEFORE void main(). The structure variables can then be defined locally in main, for example… struct Student { int rollno, age; char name[80]; float marks; }; int main() { // declare two variables of the new type Student s1, s3; ……… ……… return 0; }
  • 3. Alternate method of declaring variables of type struct: struct Student { int rollno, age; char name[80]; float marks; } s1, s3; Accessing of data members The accessing of data members is done by using the following format: structure variable.member name for example cin >> s1.rollno >> s1.age >> s1.name >> s1.marks; Initialization of structure variable Initialization is done at the time of declaration of a variable. For example Student s2 = {100, 17, "Aniket", 92}; Structure variable in assignment statement s3 = s2; The statement assigns the value of each member of s2 to the corresponding member of s3. Note that one structure variable can be assigned to another only when they are of the same structure type, otherwise complier will give an error. Nested structure (Structure within structure) It is possible to use a structure to define another structure. This is called nesting of structure. Consider the following program
  • 4. struct Day { int month, date, year; }; struct Student { int rollno, age; char name[80]; Day date_of_birth; float marks; }; Accessing Member variables of Student To access members of date_of_birth we can write the statements as below : Student s; // Structure variable of Student s.date_of_birth.month = 11; s.date_of_birth.date = 5; s.date_of_birth.year = 1999; typedef It is used to define new data type for an existing data type. It provides and alternative name for standard data type. It is used for self documenting the code by allowing descriptive name for the standard data type. The general format is: typedef existing datatype new datatype for example: typedef float real; Now, in a program one can use datatype real instead of float. Therefore, the following statement is valid: real amount;
  • 5. Enumerated data type The enum specifier defines the set of names which are stored internally as integer constant. The first name was given the integer value 0, the second value 1 and so on. for example: enum months{jan, feb, mar, apr, may} ; It has the following features:  It is user defined.  It works if you know in advance a finite list of values that a data type can take.  The list cannot be input by the user or output on the screen. #define preprocessor directive The #define preprocessor allows to define symbolic names and constants e.g. #define pi 3.14159 This statement will translate every occurrence of PI in the program to 3.14159 Macros Macros are built on the #define preprocessor. Normally a macro would look like: #define square(x) x*x Its arguments substituted for replacement text, when the macro is expanded.