STRUCTURE in C++
Hello people, in this article we are going to study about
structure in C++, we will also see the implementation of structure in C++.
Array is a collection of different variables of similar types
while on the contrary structure is a user-defined data type which is a collection
of variables of different data types.
A structure creates a data type that can be used to group
items of possibly different types into a single type.
Syntax :
The ‘struct’ keyword is used to create a structure. The
general syntax of structure is as shown below :
struct StructureName
{ member1;
member2;
member3;
member4;
.
.
.
memberN; };
Structure can contain two types of members :
1. Data Member
2. Member Functions
Implementation of structures in C++ :
Declaration
of Structure Members :
There are two ways in which we can declare the structure
variables :-
1. Declaration of structure variable with the declaration of structures.
2. Declaration of structure variable as a normal data type
Initialization
of Structure Members :
Structure members can be initialized by using curly braces ‘{}’, as shown below :
Structure members can be accessed by using dot operator (.),
as shown below :
Array of
Structure:
Like other primitive data types we can create an array of
structures. For understanding it more clearly look for the code below :
Pointer to
Structure:
Like primitive types, we can have pointer to a structure. If
we have a pointer to structure, members are accessed using arrow ( -> )
operator instead of the dot (.) operator, as shown below.
We
think, this is all we have got to offer you in this topic “structures in
C++”.
We are coming with a series of posts regarding various data structures
and their implementations in c++. Do subscribe to our daily blog update by
clicking here.
We want to acknowledge, book "Computer Fundamentals and Programming in C" by Pradip Dey, this book helped us alot during our research for this article, also this is the best one for understanding the concepts of C programming, we personally recommend you to go for this, check it out by clicking here.
Hope all your doubts regarding this are clear now.
If you still have any doubt on this
topic then do come to us via email "sophomoretechs@gmail.com" or
via instagram "@coding.winds".