0% found this document useful (0 votes)
9 views

Structures

Uploaded by

farhatshoaib19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Structures

Uploaded by

farhatshoaib19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

STRUCTURES

IZMA SHOAIB 22011598-115

MARYAM LIAQUAT 22011598-124

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.

• The data items in a structure are called


structure elements or members.

• The structure is used to define new data


types.

• Structures provide a convenient way to


organize related data and create
complex data structures.

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 closing braces are ended with a semicolon ( ; ).

• 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

 The structure variable  The definition tells


can be defined after the the compiler to
declaration of a allocate memory
structure. Syntax space for the variable.
Struct_Name .
 The process of defining Identifier;  The compiler
a structure variable is automatically
the same as defining a  Struct_Name is the allocates sufficient
variable of basic types name of the structure. memory according to
such as int and char.  Identifier is the the elements of the
variable to be defined. structure.

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.

20XX presentation title 8


Example # 1

Program that declares a structure to


store employ id and salary.t defines and
initialize structure variable. int main ()
{
#include<iostream> emp e = {20 , 18500 } ;
Using namespace std;
cout << “Employee ID : “ << e.id <<
Struct emp endl ; OUTPUT
{
int id ; cout << “ Salary : “<< e.sal << endl ; Employee ID: 20
int sal ; Return 0; Salary : 18500
}; }

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.

20XX presentation title 10


Example
#include <iostream>
// Initialize the struct members
Using namespace std;
person1.name = "John";
// Define the structure Return 0;
person1.age = 30;
struct Person }
person1.height = 1.75;
{
string name;
// Access and display the
int age; OUTPUT
struct members
float height; Name: John
cout << "Name: " <<
}; Age: 30
person1.name << endl;
Height: 1.75 meters
int main()
cout << "Age: " <<
{
person1.age << endl;
// Declare a struct variable
cout << "Height: " <<
Person person1;
person1.height << " meters" <<
endl;

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.

Referencing allows you to create an alias for the structure member,


which means that modifying the reference will affect the original
member, and vice versa.

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";

struct Person // This will also change person1.name to "Jane“


{ ageRef = 28;
std::string name;
int age; // This will also change person1.age to 28
float height; heightRef = 1.68;
};
// This will also change person1.height to 1.68
int main()
{ // Print the modified values through the struct variable
// Declare a struct variable std::cout << "Name: " << person1.name << std::endl;
Person person1; std::cout << "Age: " << person1.age << std::endl; std::cout <<
person1.name = "John"; "Height: " << person1.height << " meters" << std::endl;
person1.age = 30;
person1.height = 1.75; return 0;
}
// Create references to the struct members
std::string& nameRef = person1.name;
int& ageRef = person1.age;
float& heightRef = person1.height; THROUGH REFERENCES
// Create pointers to the struct members
Through Pointers std::string* namePtr = &person1.name;
int* agePtr = &person1.age;
#include <iostream> float* heightPtr = &person1.height;
#include <string>
// Modify the members through the pointers
struct Person
*namePtr = "Jane“;
{
std::string name; // This will also change person1.name to "Jane“
int age; *agePtr = 28;
float height;
// This will also change person1.age to 28
}; *heightPtr = 1.68;
int main()
{ // This will also change person1.height to 1.68 // Print
the modified values through the struct variable
// Declare a struct variable
std::cout << "Name: " << person1.name << std::endl;
Person person1; std::cout << "Age: " << person1.age << std::endl;
person1.name = "John"; std::cout << "Height: " << person1.height << " meters" <<
person1.age = 30;
std::endl;
person1.height = 1.75; return 0;
}

15
Thank You

You might also like