16. C-Structures

C-Strings: Previous                                                                                    Next: C-Union

What are C-Structures in programming?
The main drawback of array is, it holds a number of variables belonging to same data type. Suppose we have to store marks of 20 students then array is used but in case when we have to store name, age, marks of 20 students then array will not work because since, array stores only same type of variables and name, age, marks are different data type.
To overcome this drawback , Structure are used.
Structure definition: -
 "Structure is a user defined data type which consists number of data type together into a single unit structure can hold many data type`s variable according to user`s requirement. Only limitation of structure is memory size. The variables of structures are called members of structure."

The difference between Array and Structure: -
Array is a collection of variables having same data type while structure is a collection of variables with different data types.
How to create a Structure: -
                struct
{
;
;
----------------------;
----------------------;
----------------------;
;
};
struct’ keyword is provided by C to declare a structure.
Eg:           suppose we want to store data of a student. This can be defined by following:
struct student
{
int rollno;
char name[20];
float fees;
};
Semicolon (;) is compulsory to terminate the definition of an Structure.
The above structure is only a blue print it doesn`t exist. The size of structure is calculated by adding size of all its data members.
For Eg: in previous example size of structure will be:
int            2 bytes
char         20 bytes (1 byte for 1 character)
float         4 bytes
2 + 20 + 4 = 26 bytes in memory.
The size of structure can be seen by sizeof( ) function.
For Eg:     printf(“Size of Structure is : %d”, sizeof(student));            
Initialization of Structure: -
All the members of structure is initialized by garbage value. We can`t assign the value to members in Blue print.
Eg:           struct student
{
int rollno = 101;                       Invalid
char name[20];
};
Initialization can be done by following way:
struct student std = {1001, “Ram Kapoor”, 15000};
‘std’ is a variable of type student structure.
Three important works is associated with using structure:
  1. Declare the structure i.e. Blue print.
  2. Declare the variable of structure type.
  3. Accessing member of structure.
  1. Declare the structure: -
Defined in above section
struct student
{
int rollno;
char name[20];
};

2. Declare the variable of structure type: - declaration of structure has a no use until they come in existence. The previous declare structure can`t be used until its variable is not declared, but the syntax is same as we declare normal variable.
Syntax:    ;
This statement reserves the space in memory equal to size of structure. The member of structure rollno, name, fees are stored in main memory of computer system.
Eg:           struct student std ;
Multiple variables can also be declared for Eg: struct student std1, sd2;

3. Accessing members of structure: - [ . ]dot operator is used to access members of the structure.
Syntax:    . ;
Eg:           std.rollno = 101;
We can assign one structure variable into another structure variable.
Eg:           std1.rollno = std2.rollno;
There is also another way for declaring a structure variable :
struct student
{
int rollno;
char name[20];
float fees;
}std1,std2;
Eg: use of structure variable:
#include
void main( )
{
struct student
{
int rollno;
char name[20];
float fee;
};
struct student std;
std.rollno = 101;
std.name = “Kareena”;
std.fee = 10,000;
printf(“\n The roll number is : %d”,std.rollno);
printf(“\n The Name is : %s”,std.name);
printf(“\n The Fees is : %f”,std.fee);
}
A structure without name can also be declared.
Eg:           struct
{
char name[20];
int size
} a1, a2, a3;
In this case, structure variable can only be declared at time of structure definition.
Entering data into structure: -
In above all examples, we were not taking input from the user. The values of all data members can be taken from the user.
Eg:           #include
                void main( )
                {
                                struct student
                                {
                                                int rollno;
                                                char name[20];
                                };
                                struct student std;
                                printf(“Enter roll number : ”);
                                scanf(“%d”, &std.rollno);
                                printf(“Enter Name : ”);
                                gets(std.name);
                                printf(“\n The roll number is : %d”,std.rollno);
                                printf(“\n The Name is : %s”,std.name);
                }
Output:
program to enter data into structures
Name of data members of two structures can be same because they are local variable and their scope only within the block of structure.
For Eg: struct first
{
int a1;
float b2;
} first1;
struct second
{
int a1;
float b2;
} second1;
Nested Structure: -
We can make one structure into another structure in c programming. They enable us to create hierarchies of data items.
struct address
{
int house_no;
char house_name[20];
chat city[20];
};
struct student
{
int rollno;
char name[20];
struct address add;
};
Note: -     [ . ] dot operator is used to access the nested structure.

Defining your own data type using typedef: -
The C provides a feature to rename the basic or built in data type as well as user defined data types.
Syntax:    typedef struct
{
;
;
} ;
typedef’ keyword is used to declare data type with your own name.
typedef struct
{
char ename[20];
int deptno;
long salary;
} employee;
This above definition creates a structure type with a name “Employee”. Advantage of this is, you don`t have to use keyword struct again and again while declaring variable of structure.
Eg: employee emp;

Array of structures: -
Like a normal variables, array of structure variables can be made.
Syntax:    struct
{
;
;
};
struct [SIZE];
while working with array of structures, we have to be aware about the limitation of computers system.
Eg:           struct student
{
int rollno;
char name[20];
};
struct student std_arr[20];
the above example will store roll no and name of  20 students.
Initialization of array structure variable: -
Eg:           struct student [2] = {{101, “Amit”}, {102, “Suresh”}};
Eg: Array of Structure initialization:
#include
                struct employee
                {
                                int deptno;
                                char name[20];
                };
                struct employee emp[2] = {{10, “Ajay”}, { 20, “Vijay”}};
               
void main( )
                {
               
                                printf(“Employess Data are : ”);
                                printf(“\n Department number of Employee1 : %d”, emp[0].deptno);
                                printf(“\n Name of Employee1 : %s”, emp[0].name);
                                printf(“\n Department number of Employee2 : %d”, emp[1].deptno);
                                printf(“\n Name of Employee2 : %s”, emp[1].name);
                }
Note: Loop can be used for printing the data members of structure.
int i;
for(i = 0; i<2 i="" span="">
{
printf(“\n Department number of Employee : %d”, emp[i].deptno);
printf(“\n Name of Employee : %s”, emp[i].name);
}
Function and Structure: - We can pass structure to a function as in form of argument or parameters.
Eg:           #include
                                struct employee
                                {
                                                int deptno;
                                                char name[20];
                                };
                                struct employee emp1;
                               
                                void display(struct employee emp2)
                                {
                                                emp2.deptno = 10;
                                                emp2.name = “Salman”;
                                                printf(“\n Department number of Employee : %d”, emp2.deptno);
                                                printf(“\n Name of Employee : %s”, emp2.name);
                                }
                                void main( )
                                {
                                                display(emp1);
                                }
Output:
program to pass structure to a function

C-Strings: Previous                                                                                    Next: C-Union