Introduction to Structure and Unions
A variable stores a single value of a data type. it can stored many values of similar data types in an Array. In actual life we need to have different data types for case to maintain employees information we should have information such as name, age, qualification, salary etc. here to maintain employees dissimilar data types are required. For tackling such mixed data types, a special feature is provided by C. It is known as structure.
Definition of Structure
A structure is a collection of one or more variables of different data types, grouped together under a single name. A structure contains a number of data types grouped together. These data types may or may not be of the same type. In other words, a structure is a collection of one or more variables, possibly of different types, grouped together under a single name. The variables included in structure are called members of the structure. We cannot initialize variable in a structure. Usually structures are defined at the top of the program.
Features of Structure
To copy elements of one array to another array of same data type elements are copied one by one. It is not possible to copy all the elements at a time. Whereas in structure it is possible to copy the contents of all structure elements of different data types to another structure variable of its type using assignment (=) operator. It is possible because the structure elements are stored in successive memory location. Nesting of structures is possible. It is also possible to pass structure elements to a function. It is also possible to create structure pointers.
Declaration and Initialization of Structure
Syntax:
struct struct_name
{
type variable1;
type variable2;
};
Here, struct is a keyword. struct_name is called tag. variable1 and variable2 are the member of the structure. After defining structure we can create variables as given below.
struct struct_name v1, v2, v3.
The declaration defines the structure but this process doesn't allocate memory. The memory allocation takes places only when variables are declared.
struct book1
{
char book[30];
int page;
float price;
};
struct book1 bk1;
In the above example a structure of type book1 is created. It consists of three members book[30] of char data type, pages of int type and price of float data type.
struct book1 bk1;
The above line creates variable bk1 of type book1 and it reserves total 36 bytes (30 bytes for book [30], 2 bytes for integer and 4 bytes for float). These bytes are always in adjacent memory locations. In order to initialize structure elements with certain values following statement is used.
struct book1 bk1 = {"LALIT MATOLIYA", 500, 445.00};
We can directly assign values to members as given below
bk1.book = "LALIT MATOLIYA";
bk1.pages = 500;
bk1.price = 445.00;
The period (.) sign is used to access the structure members. If we so desire, we can combine the declaration of the structure type and the structure variables in one statement. For example,
struct book
{
char name[20];
int pages;
float price;
};
struct book bk1, bk2, bk3;
is same as...
struct book
{
char name[20];
int pages;
float price;
}bk1, bk2, bk3;
or even...
struct
{
char name[20];
int pages;
float price;
}bk1, bk2, bk3;
Syntax :
struct <structure name >
{
structure elements ;
.........................
} structure variable;
Example :
Sturct employee
{
int empno, sal;
char name [20];
}emp;
Note: Structure variable of a structure can also be declared in main ( ). Like we declare variable of any other in built data type.
Example:
struct employee emp;
We can declare any number of variables for a structure depending upon our requirements of the program.
Note the following points while declaring a structure type.
(a) The closing brace in the structure type declaration must be followed by a semicolon.
(b) Usually structure type declaration appears at the top of the source code file, before any variable or functions are defined. In very large programs they are usually put in a separate header file and the file is included using the preprocessor directive # include.
Structure initialization
Like any other data type, a structure variable can be initialized. However, a structure must be declared as static if it is to be initialized inside a function (only with old compilers).
main( )
{
struct st_record
{
int weight;
float height;
};
struct st_record student1={ 60,180.75 };
struct st_record student2={ 53, 170.60};
.........................;
.........................;
}
Iinitialize a structure variable outside the function.
struct st_record
{
int weight;
float height;
}student1 = {60,180.75};
main( )
{
struct st_record student2={ 53, 170.60};
.........................;
.........................;
}
Member variable of a structure can be accessed by using structure variable followed by dot ('.') and member variable name.
Syntax :
struct_name.variable name;
Example :
emp.name;