0% found this document useful (0 votes)
6 views17 pages

Structures Note

The document provides an overview of structures in C/C++, explaining their purpose as user-defined data types that group logically related variables of different types. It details the syntax for defining structures, declaring structure variables, initializing members, and accessing elements using the dot operator. Additionally, it includes examples and classwork assignments related to creating structures for students and patients.

Uploaded by

Deborah Sanchez
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)
6 views17 pages

Structures Note

The document provides an overview of structures in C/C++, explaining their purpose as user-defined data types that group logically related variables of different types. It details the syntax for defining structures, declaring structure variables, initializing members, and accessing elements using the dot operator. Additionally, it includes examples and classwork assignments related to creating structures for students and patients.

Uploaded by

Deborah Sanchez
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/ 17

CSC 221: COMPUTER

PROGRAMMING II
STRUCTURES
LECTURER: DR. D.D ALEBURU
⚫ What is Structure data type?
⚫ A structure is a keyword that creates user-defined data types
in C/C++.
⚫ A structure creates a data type that can be used to group
items of possibly different types into a single type.
⚫ At times, during programming, there is a need to store
multiple logically related elements under one roof.
⚫ For instance, an employee’s details like name, employee
number, and designation need to be stored together.
⚫ In such cases, the C++ language provides structures to do
the job for us.
⚫ A structure can be defined as a single entity holding variables
of different data types that are logically related to each other.
⚫ All the data members inside a structure are accessible to the
functions defined outside the structure.
⚫ To access the data members in the main function, you need
to create a structure variable.
⚫ Why Do We Use Structures in C++?
⚫ Structure in C++ programming is very helpful in cases where
we need to store similar data of multiple entities.
⚫ Let us understand the need for structures with a real-life
example.
⚫ Suppose you need to manage the record of books in a library.
Now a book can have properties like book_name, author_name,
and genre. So, for any book you need three variables to store its
records.
⚫ For example, If we want to store data on multiple patients such
as patient name, age, and blood group.
struct structName
{
/ / structure definition
Data_type1 member_name1;
Data_type2 member_name2; struct Student
Data_type2 member_name2; {
}; char name[25];
int age;
char branch[10];
char gender;
};
⚫ Syntax to Define a Structure in C++
⚫ Keyword struct: The keyword struct is used at the beginning
while defining a structure in C++.
⚫ structName: This is the name of the structure which is specified
after the keyword struct.
⚫ data_Type: The data type indicates the type of the data members
of the structure. A structure can have data members of different
data types.
⚫ member_name: This is the name of the data member of the
structure. Any number of data members can be defined inside a
structure. Each data member is allocated a separate space in the
memory.
⚫ How to Declare /tructureVariables?
⚫ Variables of the structure type can be created in C++.These
variables are allocated a separate copy of data members of the
[Link] are two ways to create a structure variable in C++.
⚫ Declaration of /tructureVariables with /tructure Definition
⚫ This way of declaring a structure variable is suitable when there
are few variables to be declared.
Syntax Example
struct structName struct bookStore
{ {
/ / structure definition / / structure definition
Data_type1 member_name1; char storeName
Data_type2 member_name2; int totalBooks;
Data_type2 member_name2; char storeLicense[20];
} struct_var1, struct_var2; } storeA, storeB; / / structure
variables
⚫ Declaration of /tructureVariables /eparately
⚫ This way of creating structure variables is preferred when
multiple variables are required to be [Link] structure
variables are declared outside the structure.
/yntax
struct structName
{
/ / structure definition Example
Data_type1 member_name1; struct bookStore
Data_type2 member_name2; {
Data_type2 member_name2; / / structure definition
}; char storeName
struct structName struct_var1, struct_var2; int totalBooks;
char storeLicense[20];
};
int main()
{
struct bookStore storeA, storeB; / / structure
variables;
}
⚫ When the structure variables are declared in the main()
function, the keyword struct followed by the structure name
has to be specified before declaring the variables. In the above
example, storeA and storeB are the variables of the structure
bookStore.
⚫ How to Initialize Structure Members?
⚫ Structure members cannot be initialized like other variables
inside the structure [Link] is because when a structure is
defined, no memory is allocated to the structure’s data members
at this point. Memory is only allocated when a structure variable
is declared. Consider the following code snippet.
struct rectangle
{
/ / structure definition
int length = 10; / / COMPILER ERROR: cannot initialize
members here.
int breadth = 6; / / COMPILER ERROR: cannot initialize
members here.
};

⚫ A compilation error will be thrown when the data members of


the structure are initialized inside the structure.
⚫ To initialize a structure’s data member, create a structure variable.
This variable can access all the members of the structure and
modify their values. Consider the following example which
initializes a structure’s members using the structure variables.
struct rectangle
{
/ / structure definition
int length;
int breadth;
};
int main()
{
struct rectangle my_rect; / / structure variables;
my_rect.length = 10;
my_rect.breadth = 6;
}
⚫ In the above example, the structure variable my_rect is
modifying the data members of the structure.
⚫ The data members are separately available to my_rect as a
copy.
⚫ Any other structure variable will get its own copy, and it will
be able to modify its version of the length and breadth.
⚫ How to Access Structure Elements?
⚫ The members of a structure are accessed outside the structure by
the structure variables using the dot operator (.).The following
syntax is used to access any member of a structure by its variable:
Syntax:

[Link]

⚫ Example
⚫ The following example illustrates how to access the members of a
structure and modify them in C++.
// Program to assign data to members of a structure variable
#include <iostream>
using namespace std;

struct Person
{
string first_name;
string last_name;
int age;
float salary;
};

int main()
{
Person p1;
cout << "Enter first name: ";
cin >> p1.first_name;
cout << "Enter last name: ";
cin >> p1.last_name;
cout << "Enter age: ";
cin >> [Link];
cout << "Enter salary: ";
cin >> [Link];
cout << "\nDisplaying Information." << endl;
cout << "First Name: " << p1.first_name << endl;
cout << "Last Name: " << p1.last_name << endl;
cout << "Age: " << [Link] << endl;
cout << "Salary: " << [Link];

return 0;
}
Classwork

• Create a structure to store students


information
• Create a structure to store patients
information

You might also like