Structures in C++ – GeeksforGeeks

We typically come round conditions the place we have to retailer a gaggle of information whether or not of comparable knowledge varieties or non-similar knowledge varieties. We have seen Arrays in C++ that are used to retailer set of information of comparable knowledge varieties at contiguous reminiscence areas.

Unlike Arrays, Structures in C++ are consumer outlined knowledge varieties that are used to retailer group of things of non-similar knowledge varieties.

What is a construction?

A construction is a user-defined knowledge kind in C/C++. A construction creates an information kind that can be utilized to group gadgets of presumably differing types right into a single kind.

Structures in C++

 

How to create a construction?

The ‘struct’ key phrase is used to create a construction. The normal syntax to create a construction is as proven beneath:

struct structureName;

Structures in C++ can include two kinds of members:

Data Memeber: These members are regular C++ variables. We can create a construction with variables of various knowledge varieties in C++.
Member Functions: These members are regular C++ features. Along with variables, we are able to additionally embody features inside a construction declaration.

Example:

int roll;

int age;

int marks;

      

void printDetails()

    cout<<"Roll = "<

    cout<<"Age = "<

    cout<<"Marks = "<

In the above construction, the info members are three integer variables to retailer roll quantity, age and marks of any pupil and the member operate is printDetails() which is printing the entire above particulars of any pupil.

 

How to declare construction variables?

A construction variable can both be declared with construction declaration or as a separate declaration like fundamental varieties.

struct Point

   int x, y;

p1; 

  

  

struct Point

   int x, y;

  

int principal()

Note: In C++, the struct key phrase is non-obligatory earlier than in declaration of a variable. In C, it’s obligatory.

How to initialize construction members?
Structure members can’t be initialized with declaration. For instance the next C program fails in compilation.

struct Point

The motive for above error is easy, when a datatype is said, no reminiscence is allotted for it. Memory is allotted solely when variables are created.

Structure members will be initialized utilizing curly braces ‘’. For instance, following is a sound initialization.

struct Point

   int x, y;

  

int principal()

 
How to entry construction parts?
Structure members are accessed utilizing dot (.) operator.

#embody

utilizing namespace std;

  

struct Point

    int x, y;

;

  

int principal()

 

What is an array of constructions?

Like different primitive knowledge varieties, we are able to create an array of constructions.

#embody

utilizing namespace std;

  

struct Point

    int x, y;

;

  

int principal()

    

    struct Point arr[10];

  

    

    arr[0].x = 10;

    arr[0].y = 20;

  

    cout << arr[0].x << " " << arr[0].y;

    return zero;

 
What is a construction pointer?
Like primitive varieties, we are able to have pointer to a construction. If we now have a pointer to construction, members are accessed utilizing arrow ( -> ) operator as a substitute of the dot (.) operator.

#embody

utilizing namespace std;

  

struct Point

    int x, y;

;

  

int principal()

What is construction member alignment?
See https://www.geeksforgeeks.org/structure-member-alignment-padding-and-data-packing/

Senior Technical Content Engineer | GeeksforGeeks

If you want GeeksforGeeks and want to contribute, you can even write an article utilizing contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article showing on the GeeksforGeeks principal web page and assist different Geeks.

Please Improve this text in case you discover something incorrect by clicking on the “Improve Article” button beneath.

Article Tags :

thumb_up
Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any concern with the above content material.

Post navigation

Previous

first_page Variables in C++