SlideShare a Scribd company logo
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

PPTX
Structure & Union in C++
PPT
pointer, structure ,union and intro to file handling
PPT
Structure c
PPT
Structure and union
PPT
Structure in c
PPTX
Structure & union
PPTX
Presentation on c programing satcture
Structure & Union in C++
pointer, structure ,union and intro to file handling
Structure c
Structure and union
Structure in c
Structure & union
Presentation on c programing satcture

What's hot (20)

PPTX
Unit 9. Structure and Unions
PPT
PDF
VIT351 Software Development VI Unit4
PDF
psnreddy php-oops
PPTX
Presentation on c structures
PPTX
Structures
PPTX
DOC
Unit 5 (1)
DOCX
Structure in c sharp
PDF
Java 1-contd
PPTX
Classes and objects1
PPTX
Class object method constructors in java
PPTX
Constructor&method
PPTX
oop lecture 3
PDF
Chapter27 polymorphism-virtual-function-abstract-class
PDF
03 structures
PPT
Java Script Introduction
PPTX
Structures,pointers and strings in c Programming
DOC
9.structure & union
PPSX
C++ programming structure & union
Unit 9. Structure and Unions
VIT351 Software Development VI Unit4
psnreddy php-oops
Presentation on c structures
Structures
Unit 5 (1)
Structure in c sharp
Java 1-contd
Classes and objects1
Class object method constructors in java
Constructor&method
oop lecture 3
Chapter27 polymorphism-virtual-function-abstract-class
03 structures
Java Script Introduction
Structures,pointers and strings in c Programming
9.structure & union
C++ programming structure & union
Ad

Viewers also liked (13)

PDF
Chapter26 inheritance-ii
PDF
Chapter22 static-class-member-example
PDF
Chapter17 oop
PDF
Chapter24 operator-overloading
PDF
Chapter21 separate-header-and-implementation-files
PDF
Chapter16 pointer
PDF
Chapter19 constructor-and-destructor
PDF
Chpater29 operation-on-file
PDF
Chapter20 class-example-program
PDF
Chapter25 inheritance-i
PDF
Chapter23 friend-function-friend-class
PDF
Revision notes for exam 2011 computer science with C++
PDF
+2 Computer Science - Volume II Notes
Chapter26 inheritance-ii
Chapter22 static-class-member-example
Chapter17 oop
Chapter24 operator-overloading
Chapter21 separate-header-and-implementation-files
Chapter16 pointer
Chapter19 constructor-and-destructor
Chpater29 operation-on-file
Chapter20 class-example-program
Chapter25 inheritance-i
Chapter23 friend-function-friend-class
Revision notes for exam 2011 computer science with C++
+2 Computer Science - Volume II Notes
Ad

Similar to Chapter15 structure (20)

PPTX
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
PPTX
User defined data types.pptx
PPTX
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
PPTX
Structure prespentation
PDF
Chapter 13.1.9
PDF
Lk module4 structures
PDF
Structure In C
PDF
unit 5.pdf structure pdf is here you can do this
DOCX
Str
PPT
Structures
PPTX
Data Structures and Algorithms_Updated.pptx
PPTX
Chapter 2 part II array and structure.pptx
PPT
Unit4 (2)
PPTX
12Structures.pptx
PDF
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
PPT
Lecture number three Structures (1).ppt
PPTX
detail structure presentation of problem solving
DOCX
Structure and Typedef
PPTX
Cs1123 12 structures
PPTX
Structure.pptx Structure.pptxStructure.pptxStructure.pptxStructure.pptxStruct...
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
User defined data types.pptx
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Structure prespentation
Chapter 13.1.9
Lk module4 structures
Structure In C
unit 5.pdf structure pdf is here you can do this
Str
Structures
Data Structures and Algorithms_Updated.pptx
Chapter 2 part II array and structure.pptx
Unit4 (2)
12Structures.pptx
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
Lecture number three Structures (1).ppt
detail structure presentation of problem solving
Structure and Typedef
Cs1123 12 structures
Structure.pptx Structure.pptxStructure.pptxStructure.pptxStructure.pptxStruct...

More from Deepak Singh (17)

PDF
Computer networks - CBSE New Syllabus (083) Class - XII
PDF
Chapter28 data-file-handling
PDF
Chapter18 class-and-objects
PDF
Chapter13 two-dimensional-array
PDF
Chapter12 array-single-dimension
PDF
Chapter 11 Function
PDF
Chapter 10 Library Function
PDF
Chapter 9 - Loops in C++
PDF
Chapter 8 - Conditional Statement
PDF
Chapter 7 - Input Output Statements in C++
PDF
Chapter 5 - Operators in C++
PDF
Chapter 3 - Variable Memory Concept
PDF
Chapter 2 - Structure of C++ Program
PPT
Inheritance polymorphism-in-java
PDF
Computer science-2010-cbse-question-paper
PDF
Cbse question-paper-computer-science-2009
PDF
CBSE Question Paper Computer Science with C++ 2011
Computer networks - CBSE New Syllabus (083) Class - XII
Chapter28 data-file-handling
Chapter18 class-and-objects
Chapter13 two-dimensional-array
Chapter12 array-single-dimension
Chapter 11 Function
Chapter 10 Library Function
Chapter 9 - Loops in C++
Chapter 8 - Conditional Statement
Chapter 7 - Input Output Statements in C++
Chapter 5 - Operators in C++
Chapter 3 - Variable Memory Concept
Chapter 2 - Structure of C++ Program
Inheritance polymorphism-in-java
Computer science-2010-cbse-question-paper
Cbse question-paper-computer-science-2009
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.