Chapter 4 STR
Chapter 4 STR
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.
• 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.