- C++ Course Basics
- C++ Tutorial
- C++ Basic Syntax
- C++ Identifiers
- C++ Character Set
- C++ Input/Output Operator
- C++ Variables
- C++ Data Types
- C++ Formatting Output
- C++ Operators
- C++ Assignment Operator
- C++ Type Conversion
- C++ Program Control
- C++ if and if-else
- C++ switch
- C++ loops
- C++ break and continue
- C++ Functions
- C++ Functions
- C++ Prototype and Definition
- C++ Function Call
- C++ Function Types
- C++ Friend Function
- C++ Function Overloading
- C++ Arrays and Strings
- C++ Arrays
- C++ One-Dimensional Arrays
- C++ Strings
- C++ String Functions
- C++ Structures
- C++ Structures
- C++ Nested Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ Pointers
- C++ Pointers
- C++ Memory Map
- C++ Declare Initialize Pointers
- C++ Pointers and Structures
- C++ Object-Oriented
- C++ Object-Oriented
- C++ Classes and Objects
- C++ Constructors and Destructors
- C++ Objects as Function Arguments
- C++ Pointers and Objects
- C++ Data Structure
- C++ Linked List
- C++ Stack
- C++ Queues
- C++ File Handling
- C++ File Handling
- C++ Opening and Closing Files
- C++ Steps to Process Files
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Misc
- C++ typedef
- C++ #define
- C++ Date and Time
- C++ Examples
- C++ Examples
Structures in C++ Programming with Examples
This article will teach you about "C++ structures" or "C++ struct keyword" in detail and with examples. So, without further ado, let's get started.
Structures are created in C++ using the "struct" keyword.
When we need to group multiple variables of the same or different data types into a single location, we use a structure. Structures, as opposed to arrays, are used to declare multiple variables without regard for their data types. In addition, we use the structure variable to initialize the structure member (the variables declared within the structure) and use it in the program. Don't worry, you'll get it eventually if you keep reading the post.
Create a structure in C++
To create a structure in C++, follow the following general form:
struct { dataType variableName1; dataType variableName2; dataType variableName3; . . . dataType variableNameN; } structureVariableName;
The keyword "struct" is used to declare a structure in C++, "dataType" is a valid C++ data type, "variableName1," "variableName2," "variableName3," and "variableNameN" are the names of variables that are also known as structure members, and "structureVariableName" is the name of the structure variable that will be used to access the structure members in the program to initialize and use them. As an example,
struct { int sno; long int empId; string empName; string empCity; } fresherearth;
The preceding C++ code fragment declares a structure with four members, namely "sno," "empId," "empName," and "empCity," of the types "int," "long int," "string," and "string," as well as a structure variable called "fresherearth."
We can have multiple structure variables as well, for example.
struct { int sno; long int empId; string empName; string empCity; } a, b, c, d, e;
There is another way to create a structure in C++, which is by creating the structure along with its name or tag. The following is the general form:
struct name { type variable1; type variable2; type variable3; } structureVariables;
Through this approach, we can also create structure variables later in the program. You will see an example of this concept later in this post.
Access structure members in C++
To gain access to structure members, use the following general syntax:
structureVariable.structureMember
As an example,
fresherearth.sno = 4;
The preceding C++ statement accessed the structure's member "sno," whose variable is "fresherearth."
C++ structure example program
Now that I've defined how to declare, access, and initialize structure, it's time to create and explain a complete example program demonstrating the structure. As a result, I wrote the following program to serve as an example of the "C++ structure."
#include<iostream> using namespace std; struct { int sno; long int empId; string empName; string empCity; } xyz; int main() { xyz.sno = 1; xyz.empId = 123904; xyz.empName = "William"; xyz.empCity = "Houston"; cout<<"S.No. = "<<xyz.sno<<endl; cout<<"ID = "<<xyz.empId<<endl; cout<<"Name = "<<xyz.empName<<endl; cout<<"City = "<<xyz.empCity<<endl; return 0; }
The output produced by this C++ program illustrating the structures should exactly be:
S.No. = 1 ID = 123904 Name = William City = Houston
Now let me allow the user to define the values at program runtime. The following program not only allows the user to enter the values; it also uses multiple structure variables.
#include<iostream> using namespace std; struct { int sno; long int empId; string empName; string empCity; } a, b; int main() { cout<<"----Enter the data for the first employee----\n"; cout<<"Enter the Serial Number: "; cin>>a.sno; cout<<"Enter the ID: "; cin>>a.empId; cout<<"Enter the Name: "; cin>>a.empName; cout<<"Enter the City: "; cin>>a.empCity; cout<<"\n\n----Enter the data for the second employee----\n"; cout<<"Enter the Serial Number: "; cin>>b.sno; cout<<"Enter the ID: "; cin>>b.empId; cout<<"Enter the Name: "; cin>>b.empName; cout<<"Enter the City: "; cin>>b.empCity; cout<<"\n\nData\t\tEmployee 1\t\tEmployee 2"; cout<<"\nS.No.\t\t"<<a.sno<<"\t\t\t"<<b.sno; cout<<"\nID\t\t"<<a.empId<<"\t\t\t"<<b.empId; cout<<"\nName\t\t"<<a.empName<<"\t\t\t"<<b.empName; cout<<"\nCity\t\t"<<a.empCity<<"\t\t\t"<<b.empCity; cout<<endl; return 0; }
The snapshot given below shows a sample run of the above program:
In the above program, the "\n" is used to insert a line break, and "\t" is used to insert a horizontal tab on the output console. The "\n" and endl, both used to insert a line break.
We can, however, use an array instead of multiple structure variables. The explanation can be found in the "C++ Structure Array" post.
Now, let's create the same program using the second method of declaring and defining the structure, which is by using its name and declaring its variable later in the program; that is, I will declare the structure variable using its name or tag inside the "main()" method.
#include<iostream> using namespace std; struct myStructure { int sno; long int empId; string empName; string empCity; } ; int main() { myStructure a, b; cout<<"----Enter the data for the first employee----\n"; cout<<"Enter the Serial Number: "; cin>>a.sno; cout<<"Enter the ID: "; cin>>a.empId; cout<<"Enter the Name: "; cin>>a.empName; cout<<"Enter the City: "; cin>>a.empCity; cout<<"\n\n----Enter the data for the second employee----\n"; cout<<"Enter the Serial Number: "; cin>>b.sno; cout<<"Enter the ID: "; cin>>b.empId; cout<<"Enter the Name: "; cin>>b.empName; cout<<"Enter the City: "; cin>>b.empCity; cout<<"\n\nData\t\tEmployee 1\t\tEmployee 2"; cout<<"\nS.No.\t\t"<<a.sno<<"\t\t\t"<<b.sno; cout<<"\nID\t\t"<<a.empId<<"\t\t\t"<<b.empId; cout<<"\nName\t\t"<<a.empName<<"\t\t\t"<<b.empName; cout<<"\nCity\t\t"<<a.empCity<<"\t\t\t"<<b.empCity; cout<<endl; return 0; }
This program does the same job as the previous program.
I only used two structure variables to keep the program short, understandable, and entertaining. You can, however, use as many variables as you want. However, if you want to use multiple structure variables, I recommend using an array.
« Previous Tutorial Next Tutorial »