Structures
Structures
Structures
UBAIDULLAH 22011598-139
Introduction to
Structures
In C++, the term "structure" refers to a
user-defined data type that allows you to
combine data types into a single entity.
3
Declaring a Structure
A structure is declared by using the keyword struct followed by the structure name.
• The structure members are defined with their type inside the opening and closing braces { }.
• The declaration tells the compiler about the details of the structure. How to declare a
structure ???
4
Syntax Pictorial Representation
struct: This keyword is used to
define a structure in C++.
Struct_Name: This is the name of
the structure you are defining. You
can choose any valid identifier as
the structure name.
Data_Type1, data_Type2, etc.:
These represent the data types of
the members within the structure.
member1, member2, etc.: These
are the individual members of the
structure. You can have multiple
members, each with its own data
type.
5
Example
• The following example declares a structure Student with four members variables;
struct Student
{
int RollNo, Mark;
float Average;
char Grade;
}
6
Defining Structure Variable
7
Initializing
Structure
Variables • SYNTAX
The process of assigning values to
member variables of a structure is Struct_Name Identifier = { Value1 , Value2 } ;
called the Initialization of Structure Value1 and Value2 are the values used for the initialization of the
Variables. structure variable.
The assignment operator is used for
initialization.
The declaration of a structure
variable is specified on the left side
of assignment operator.
The value for initialization are
written on the right side of
assignment operator.
The values are written in braces { }
Each value is separated by a
comma.
9
Accessing members of Structure Variable
Any member of a structure variable can be accessed by using the dot operator.
The dot operator allows you to access the individual members (fields) of the
structure and manipulate their values.
The name of the structure variable is written on the left side of the dot.
The name of the member variable is written on the right side of the dot.
11
Example # 2
#include <iostream>
Cout<<“enter marks:” ;
Using namespace std;
Cin>> s.marks;
// Define the structure Return 0;
Cout<<“enter average :” ; }
struct Student
Cin>> s.average ;
{
string name;
Cout<<“you entered the OUTPUT
int marks;
following details” ; Enter name: John
float average;
}; Enter marks:872
cout<<“name”<<s.name<<endl; Enter average:76.53
Cout<<“marks<<s.mamrks
int main()
<< endl; You entered following
{
Cout<<“average”<<s.average details
<< endl; Name : John
Student s;
Cout<<“enter name:” ; Marks: 872
Cin>>s.name ; Average: 76.53
12
Referencing Structure Member
In C++, you can refer structure members using references or pointers.
This can be useful when you want to work with structure members
directly and avoid copying large structures.
13
include <iostream> // Modify the members through the references
#include <string> nameRef = "Jane";
15
Thank You