Monday, March 23, 2020

Structures in c++

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.
                       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?
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.

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; 
}

30 comments:

  1. very well explained in precise words!

    ReplyDelete
  2. Very neatly explained. Good work
    Expecting more to come along

    ReplyDelete
  3. Wow that nicely explained.Loved your data and structure ๐Ÿ™Œ

    ReplyDelete
  4. Well explained..good to understand๐Ÿ‘

    ReplyDelete
  5. So I have to use arrow operator for structure member

    ReplyDelete
  6. Well explained.... Easy to understand

    ReplyDelete
  7. Efforts 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 ๐Ÿ’ฅ๐Ÿ’ฅ๐Ÿ’ฅ

    ReplyDelete
  8. Well explained! ...Nice work!

    ReplyDelete
  9. Well explained...my knowledge increased drastically

    ReplyDelete

DOCUMENTATION

Course project Title:      Implementation of Family tree Using Generalized Link List Description:    In this project we have create...