Structures in C Programming Language

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.
Structure
In C, a structure is a derived data type consisting of a collection of member elements and their data types. Thus, a variable of a structure type is the name of a group of one or more members which may or may not be of the same data type. In programming terminology, a structure data type is referred to as a record data type and the members are called fields.
Read Also:
  1. Memory Allocation
  2. Data Structures
  3. Preprocessor Directives
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:

Each member in the structure may belong to a different type of data.
Declaring structure:- To declare variables of a structure data type, you use the keyword struct followed by the struct tag, and then the variable name.
For example, the statements below declares a structure data type and then uses the student struct to declare variables B1 and B2.
The tag name may be used subsequently to declare variables that have the tag's structure.
It includes the following elements:
  • The keyword struet. 
  • The structure tag name. 
  • List of variable names separated by commas.
  • A terminating semicolon. 
For Example:

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 information
to 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:

Post a Comment

0 Comments