Difference Between C++ Simple Variable and Structure Variable

- June 23, 2018
A variable represents a location in the memory of computer. During program execution, it is used to store input data and its operations. The name of the memory location (such as variable name) remains fixed, whereas data of the variable may change several times during the program execution.

A structure is a collection of related data items. The data items are known as members or elements or fileds of the structure. A structure is unlike an array. In an array, all elements are of same type whereas the elements can be various types in the structure.

The structure that is used to define user defined data types or variables is called structure variable.
The main difference between variable and structure variable is as follows:
Variable Structure Variable
A simple variable can store only one data value. A structure variable can store multiple data values.
It can store one type of data at a time. It can be store various kinds of data types.

Difference in Declaration

C++ is a typed language that means all variables must be declared before they are used in the program. The C++ compiler will give an error if an undeclared variable used in the program. In C++, variables can be declared anywhere in the program before their use. A simple variable can be declared as follows:
 dt var_name;
Where:
dt It represents the data type of variables. It may be int, float, double, char etc.
var_name It indicate the name of the variable.
In the matter of structure variable, a structure must be defined before using structure variables. In declaration of structure variable, the tag name of defined function is used to specify the data type.
The syntax to declare structure variable is given below:
 tag_name    var_name1, var_name2;
Where:
tag_name It represents the structure name that is already created.
var_name1, var_name2 They indicate the variables name of same structure type that are written separated with commas.

Leave your reply.
EmoticonEmoticon