Overview
Structure is a template used to create a user-defined data type. This means that, as we create the variables of built-in data types, such as int, float, and double, we can now, also create the variables of user-defined data types, provided that the template for this data type are defined by creating a structure in the program. Structure is a way to combine the logically related data elements into a single unit. Each of the data elements defined inside the structure, are called the members of a structure. The members defined inside the structure can be of a same or different data types. Hence, a structure can be defined as a template which is created for storing heterogeneous values in physically adjacent memory locations.
What is a “user-defined” data type?
C/C++ has a built-in set of primitive data types like int, float, char, double etc, which we can be directly used in the program. We can create variables of built-in types and perform operations with the values stored in these variables. Along with using built-in data types, we can also create customized data types in a program. “User-defined data type” is a “customized” data type that the programmer can create as per the requirements to store data in computer memory.
Structures are used to create user-defined data types.
What is the need to create user-defined data types in the program?
Let us assume that you need to store data about the vehicles assembled in an automobile industry. The relevant information that you wish to maintain for each vehicle is “colour”, “no of seats”, “type of fuel” and “mileage” of the vehicle. As and when a new car is assembled you will need to create 4 different variables to store its information in computer memory. Instead, it would be easy if the programmer has an ability to create a variable of type Car using the statement,
Car c1;
where cl stores all the required information about the car in a real world. The variable cl can internally store the 4 attributes “colour”, “no of seats”, “type of fuel” and “mileage” of the vehicle.
Review the options below to login to check your access.
Log in with your Cambridge Aspire website account to check access.
If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.