Structure:-
So far, we have seen one kind of user defined data type the array in array chapter and we have seen how we can group information into one common data structure. However, the use of arrays is Defining a Structure.Read Also:
In Other word, Structure is a user-defined data type which allows us to combine data at different types together, Structures are used to represent a record. Structure use struct keyword to hold the information of structure member or elements.
For defining the structures, we must specify the names and types of each of the fields of the structure and declare variables of that type.
The general syntax of a structure definition is as follows:
Initialization:-
A structure variable can be initialized at compile time.For example:
In the give program the value 60 is assigned to student.weight and 180.75 is assigned to student.height at compile time. There is a one-to- one corresponding between the members and their initializing values.
Accessing Structure Members:-
Variables are linked to the structure variables in order to make them meaningful members. The link between a member and a variable is established using the member operator which is also known as 'dot operator' or 'period operator'.You can access Structure members for 2 type. Either you access the structure members to assign values to the members or to print their values as output. Whenever you access any structure member, you do so by (.) Dot operator.
Suppose you want to assign values to the variables of books structure, then you can do it this way.
If you want to print the variables of books structure as output then you can do this in this way.
Passing Structure To A Function:-
There are several different ways to pass structure type informationto or from a function. Structure member can be transferred individually, or entire structure can be transferred. The individual structures members can be passed to a function as arguments in the
function call and a single structure member can be returned via
the return statement. To do so, each structure member is treated
the same way as an ordinary, single valued variable.
A complete structure can be transferred to a function by passing a
structure type pointer as an argument. It should be understood
that a structure passed in this manner will be passed by reference
rather than by value. So, if any of the structure members are altered
within the function, the alterations will be recognized outside the
function.
Let us consider the following example:
Array Of Structures:-
An array of structure is defined whenever the same structure is to be applied to a group of items, elements, In array of structure each element is the structure in itself. In array of structures each element of the array represents a structure variable.Array of structure can be declared as:
struct class student[100];
defines an array called student, that consists of 100 elements. Each element is defined to be of the type struct class. An array of structures is stored inside the memory in the same way as a multi-dimensional array.
For example:
This declares the student as an array of three elements student[0], student[1], and student[2] and initializes their members as follow:
0 Comments