Demonstration of new data types using structures

What is a structure?
A structure is a user defined data type in C/C++. 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?
‘struct’ keyword is used to create a structure. Following is an example.

the below example demonstrates the creation of new data type point which can store coordinates :


// A variable declaration with structure declaration. 
struct Point 
{ 
   int x, y; 
} p1;  // The variable p1 is declared with 'Point' 
  

for using the above structure we need to use structure_name (.) member access operator followed by a member name of defined structure.
int main() 
{ 
   // A valid initialization. member x gets value 0 and y 
   // gets value 1.  The order of declaration is followed. 
   struct Point p1 = {0, 1};  
   cout<<"value of x="<<p1.x<<"value of y="<<p1.y;
}

 if any doubts or queries please comment 🙂

Comments

Popular posts from this blog

Python and C++ program to implement multiplication of 2d array (Matrix multiplication)

How to find and replace a node in Linked List

What is AI (Artificial Intelligence )? and its characteristics