Structure in C or C++ with syntax and example

Structures are special technique to store variables of various data types under a common name. Using structures, we can make our program modular and simpler to implement. Structures makes the program design more compressed, thus making it easier to find errors.
Structures are generally useful whenever a lot of data needs to be grouped together.
For example, structures can be used to hold records from a database or keep a track of your Books in a library. Consider an example of a car manufacturing company which has to maintain a record of the orders they need to deliver to the dealers in different locations. So it is obvious that you need to know either of the following attributes about each car in order to know the rest
  • Make
  • Model
  • Color
  • Location of the Dealer
  • Booking ID
C/C++ arrays allow you to define variables that combine several data items of the same kind and structure is another user defined data type which allows you to combine data items of different kinds.

Defining a Structure:

In C or C++ programming language, to declare and define a structure, the “struct” keyword must be used. The “struct” statement defines a new data type (the structure name , also called a tag) with all the member defined in between {}, followed by a semicolon ‘;’ at the end. A structure variable can be declared after closing the structure body (just before the final semicolon).
The two general formats to define a structure is as follows:

structures without Structure variable:

struct StructureName
{

//Variable declarations
member1 declarations;
member2 declarations;
...
memberN declarations;
};

structures with a Structure variable:

One or more structure variables can be specified, but it is optional
struct StructureName
{
//Variable declarations
member1 declarations;
member2 declarations;
...
memberN declarations;
}StructVariable;


The “StructureName” is acting as a data type to all its members declarations such as int i; or float f; or any other valid variable definition. Thus all its members are now of StructureName type. The StructVariable is used to access the members of the structure which has been discussed further.

Example of Structure:

Here is the way you would declare the CarDetails structure:
struct CarDetails
{
char Make[25];
char Model[10];
char Color[10];
int BookingId;
}car1, car2;
Here CarDetails is our structure’s name, the Make, Model, Color, CarDetailsBookingId are its member variables which are now of the type CarDetails. The car1, car2 are the Structure access variables.

Accessing Structure Members:

To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use struct keyword to define variables of structure type. Following is the example to explain usage of structure:

#include <iostream.h>
#include <cstring.h.h>
using namespace std;

struct CarDetails
{

char Make[25];
char Model[10];
char Color[10];
int BookingId;
};
int main( )
{
struct CarDetails car1; // Declare car1 of type CarDetails
struct CarDetails car2; // Declare car2 of type CarDetails

// car 1 specification
strcpy( CarDetails.Make, "Sedan");
strcpy( CarDetails.Model, "Swift");
strcpy( CarDetails.Color, "Beige");
CarDetails BookingId = 649047;

// CarDetails 2 specification
strcpy( CarDetails2.Make, "Telecom Billing");
strcpy( CarDetails2.Model, "Yakit Singha");
strcpy( CarDetails2.Color, "Telecom");
CarDetails2BookingId = 6495700;

// Print CarDetails info
cout << "CarDetails 1 Make : " << CarDetails .Make < cout << "CarDetails 1 Model : " << CarDetails .Model < cout << "CarDetails 1 Color : " << CarDetails .Color < cout << "CarDetails 1 id : " << CarDetails BookingId <
// Print CarDetails2 info
cout << "CarDetails 2 Make : " << CarDetails2.Make < cout << "CarDetails 2 Model : " << CarDetails2.Model < cout << "CarDetails 2 Color : " << CarDetails2.Color < cout << "CarDetails 2 id : " << CarDetails2BookingId;
return 0;

}


When the above code is compiled and executed, it produces the following result:
CarDetails 1 Make : Learn C++ Programming
CarDetails 1 Model : Chand Miyan
CarDetails 1 Color : C++ Programming
CarDetails 1 id : 6495407
CarDetails 2 Make : Telecom Billing
CarDetails 2 Model : Yakit Singha
CarDetails 2 Color : Telecom
CarDetails 2 id : 6495700

Using typedef Keyword to declare structure

There is an easier way to define structs or you could "alias" types you create. For example:
typedef struct
{
char Make[50];
char Model[50];
char Color[100];
int BookingId;
}CarDetails;

Now, you can use CarDetails directly to define the variables of CarDetails type without using struct keyword. Following is the example:
CarDetails CarDetails1, CarDetails2;

Comments

Popular posts from this blog

Samsung Galaxy Note 3 - Full Specifications

Run Borland Turbo C/C++ in Android Devices