Introduction to STRUCTURE in C++
A structure is a collection of variables under a single name. Variables can be of any type like int, float, char etc. The main difference between structures and array is that arrays are collections of the same data types and structure is a collection of variables of different data type under a single name or they store heterogeneous data in it.
A structure declaration forms a template that may be used to create structure objects. The variables that make up the structure are called members. All of the members of the structure are logically related.
Definition of STRUCTURE: A structure is a user-defined datatype which is created by a collection of heterogeneous elements represented by the same name.
Declaration of STRUCTURE:
The structure is declared by using the keyword struct followed by the structure name, also called a tag. Then the structure members are defined with their type and variable names inside the open and closing braces "{" and "}". Finally, the closed braces end with a semicolon denoted as ";" following the statement. The above structure declaration is also called a structure specifier.
Syntax of STRUCTURE:
struct structure_name
{
data_type structure_members;
......
......
};
Example of structure:
struct employee
{
int imp_id;
int salary;
float commission;
};
In the above example, the variables of different types such as int and float are grouped in a single structure name Employee. Arrays behave in the same way, declaring structures does not mean that memory is allocated. Structure declaration gives a skeleton for the structure.
Thanks
Mukesh Rajput
Post A Comment:
0 comments: