As we are going to implement link list in our project, let us understand some basic concept
of structures in c++.
We often come around situations where we need to store a group of data whether of similar data types or non-similar data types. Arrays in C++ which are used to store set of data of similar data types at contiguous memory locations.
of structures in c++.
We often come around situations where we need to store a group of data whether of similar data types or non-similar data types. 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 creates a data type that can be used to group items of possibly different types into a single type.
How to create a structure?
How to create a structure?
The ‘struct’ keyword is used to create a structure. The general syntax to create a structure is as shown below:
struct structureName
{ member1;
member2;
member3;
.
.
.
.
.
memberN;
};
Structures in C++ can contain two types of members:
- Data Member: These members are normal C++ variables. We can create a structure with variables of different data types in C++.
- Member Functions: These members are normal C++ functions. Along with variables, we can also include functions inside a structure declaration.
How to declare structure variables?
A structure variable can either be declared with structure declaration or as a separate declaration like basic types.
A structure variable can either be declared with structure declaration or as a separate declaration like basic types.
struct Point
{
int x, y;
} p1; // The variable p1 is declared with 'Point'
// A variable declaration like basic data types
struct Point
{
int x, y;
};
int main()
{
struct Point p1;
}
How to initialize structure members?
Structure members can be initialized using curly braces ‘{}’.
For example,
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
};
How to access structure elements?
Structure members are accessed using dot (.) operator.
#include <iostream>
using namespace std;
struct Point {
int x, y;
};
int main()
{
struct Point p1 = { 0, 1 };
p1.x = 20;
cout << "x = " << p1.x << ", y = " << p1.y;
return 0;
}
What is a structure pointer?
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.
#include <iostream>
using namespace std;
struct Point {
int x, y;
};
int main()
{
struct Point p1 = { 1, 2 };
cout << p2->x << " " << p2->y;
return 0;
}
Nice๐
ReplyDeletevery well explained in precise words!
ReplyDeleteVery neatly explained. Good work
ReplyDeleteExpecting more to come along
Thanks means a lot!
DeleteWow that nicely explained.Loved your data and structure ๐
ReplyDeleteGreat work ๐
ReplyDeleteThanks!
DeleteWell explained
ReplyDeleteWell explained
ReplyDeleteThanks for this informative blog.
ReplyDeleteThanks!๐
DeleteNice ๐๐ป
ReplyDeletegood work
ReplyDeleteWell explained..good to understand๐
ReplyDeletesimple and easy explanation!!!
ReplyDeleteGood one.
ReplyDeleteThanks
DeleteSo I have to use arrow operator for structure member
ReplyDeleteEasy to understand
DeleteInformative
ReplyDeleteWell explained.... Easy to understand
ReplyDeleteEfforts taken by team can be seen in this article.... Good job guys a lot of tricky concept are cleared by this short but sweet article ๐๐๐๐ keep it up ๐ฅ๐ฅ๐ฅ
ReplyDeleteNice๐
ReplyDeletewell explaind๐
ReplyDeleteWell done
ReplyDeletegood work man !
ReplyDeleteWell explained! ...Nice work!
ReplyDeleteWell explained...my knowledge increased drastically
ReplyDeleteKnowledgeable work๐
ReplyDeleteUseful content
ReplyDelete