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

Chapter 4 STR

Chapter 4 discusses the concept of structures in C++, which are user-defined data types that allow for the grouping of items of different data types. It explains how to declare and initialize structures, as well as how to access their members using pointers. The chapter emphasizes the advantages of using structures for organizing related data in a more readable and efficient manner.

Uploaded by

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

Chapter 4 STR

Chapter 4 discusses the concept of structures in C++, which are user-defined data types that allow for the grouping of items of different data types. It explains how to declare and initialize structures, as well as how to access their members using pointers. The chapter emphasizes the advantages of using structures for organizing related data in a more readable and efficient manner.

Uploaded by

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

Chapter 4

STRUCTURE

Prepared by Hana .M Saturday, 1


April 5, 20
25
INTRODUCTION

• • We often come around situations where we need to store a group of data


• whether of similar data types or non-similar data types.
• • We have seen Arrays in C++ which are used to store set of data of similar
• data types at contiguous memory locations.
• • Unlike Arrays, Structures in C++ are user defined data types which are used
• to store group of items of non-similar data types.
• • A structure is a user-defined data type in C/C++.
• • A structure creates a data type that can be used to group items of possibly
• different types into a single type.
Prepared by Hana .M Saturday, April 5, 202 2
5
Structure
• For example: You want to store some information about a person:
• o his/her name, citizenship number and salary
• • You can easily create different variables name, citNo, salary to store these information
separately.
• • A better approach will be to have a collection of all related information under a single name
• Person, and use it for every person. Now, the code looks much cleaner, readable and efficient as
• well.
• • This collection of all related information under a single name Person is a structure.

Prepared by Hana .M Saturday, April 5, 202 3


5
DECLARING A STRUCTURE

• • The struct keyword defines a structure type followed by an identifier (name of the structure).
• • Then inside the curly braces, you can declare one or more members (declare variables inside
• curly braces) of that structure. For example:
• struct Person
• {
• char name[50];
• int age;
• float salary;
• };
• • Here a structure person is defined which has three members: name, age and salary.
• • When a structure is created, no memory is allocated.

Prepared by Hana .M Saturday, April 5, 202 4


5
Structure definition

• The structure definition is only the blueprint for the creating of variables.
• • You can imagine it as a datatype. When you define an integer as below:
• int area;
• • The int specifies that, variable foo can hold integer element only. Similarly,
structure definition
• only specifies that, what property a structure variable holds when it is
defined.
• • Note: Remember to end the declaration with a semicolon (;)
Prepared by Hana .M Saturday, April 5, 202 5
5
Structure definition
• #include <iostream>
• #include <string>
• struct Person {// Define the Person structure
• std::string name;
• int age;
• float salary;
• };
• int main() {
• // Define a variable of type Person
• Person person1;
Prepared by Hana .M Saturday, April 5, 202 6
5
INITIALIZING STRUCTURE
• You can initialize a structure at the time that it is
declared. To give a structure variable a value, you follow it
• by an equal sign and a list of the member values enclosed
in braces.

Prepared by Hana .M Saturday, April 5, 202 7


5
INITIALIZING AND ACCESS STRUCTURE

• struct Person {// Define the Person structure


• char name[50];
• int age;
• float salary;
• };
• int main() {
• struct Person person1 = {"John Doe", 30, 50000.0}; // Initialize a structure variable at declaration
printf("Name: %s\n", person1.name); // Display or access the initialized structure
• printf("Age: %d\n", person1.age);
• printf("Salary: %.2f\n", person1.salary);
• return 0;
• }

Prepared by Hana .M Saturday, April 5, 202 8


5
Prepared by Hana .M Saturday, April 5, 202 9
5
Pointer to Structure
• struct Person {// Define the Person structure
• std::string name;
• int age;
• float salary;
• };
• int main() {
• Person person = {"Bob", 30, 70000.0}; // Declare a structure variable
• Person *personPtr; // Declare a pointer to a Person structure
• personPtr = &person; // Assign the address of person to the pointer
• std::cout << "Name: " << personPtr->name << std::endl; // Accessing structure members using the pointer
• std::cout << "Age: " << personPtr->age << std::endl;
• std::cout << "Salary: " << personPtr->salary << std::endl;
• return 0;}

Prepared by Hana .M Saturday, April 5, 202 10


5
Prepared by Hana .M Saturday, April 5, 202 11
5

You might also like