C Programming Language learn c tutorial

Structure

                      A structure is a user defined data type. We know that arrays can be used to represent a group of data items that belong to the same type, such as int or float. However we cannot use an array if we want to represent a collection of data items of different types using a single name. A structure is a convenient tool for handling a group of logically related data items.

The syntax of structure declaration is
struct structure_name
         {
         type element 1;
         type element 2;
         ……………..
         type element n;
         };

                       In structure declaration the keyword struct appears first, this followed by structure name. The member of structure should be enclosed between a pair of braces and it defines one by one each ending with a semicolon. It can also be array of structure. There is an enclosing brace at the end of declaration and it end with a semicolon.

We can declare structure variables as follows
                          struct structure_name var1,var2,…..,var n;

For Example:
                          To store the names, roll number and total mark of a student you can declare 3 variables. To store this data for more than one student 3 separate arrays may be declared. Another choice is to make a structure. No memory is allocated when a structure is declared. It just defines the “form” of the structure. When a variable is made then memory is allocated. This is equivalent to saying that there's no memory for “int” , but when we declare an integer that is. int var; only then memory is allocated. The structure for the above-mentioned case will look like

struct student
           {
           int rollno;
           char name[25];
           float totalmark;
           };

We can now declare structure variables stud1, stud2 as follows
                            struct student stud1,stud2;
Thus, the stud1 and stud2 are structure variables of type student. The above structure can hold information of 2 students.

                            It is possible to combine the declaration of structure combination with that of the structure variables, as shown below.
struct structure_name
          {
          type element 1;
          type element 2;
           ……………..
          type element n;
          }var1,var2,…,varn;

                               The following single declaration is equivalent to the two declaration presented in the previous example.
struct student
           {
           int rollno;
           char name[25];
           float totalmark;
           } stud1, stud2;

                                The different variable types stored in a structure are called its members. The structure member can be accessed by using a dot (.) operator, so the dot operator is known as structure member operator.

Example:
                                In the above example stud1 is a structure variable of type student. To access the member name, we would write
                                 stud1.name
Similarly, stud1’s rollno and stud1’s totalmark can be accessed by writing
                                stud1.rollno                  And
                                stud1.totalmark

Initializing Structure Members

Structure members can be initialized at declaration. This much the same manner as the element of an array; the initial value must appear in the order in which they will be assigned to their corresponding structure members,enclosed in braces and seperated by commas .The general form is

struct stucture_name  var={val1,val2,val3…..};

Example:
#include  <stdio.h>
#include<conio.h>
int main()
      {
      struct student
                {
                char *name;
                int rollno;
                float totalmark;
               };
      struct student stud1={"Ashraf",1,98};
      struct student stud3= {"Rahul",3,97};
      struct student stud2={"Vineeth",2,99};
      clrscr();
      printf("STUDENTS DETAILS:\nRoll number:%d\n\nName:%s\n\nTotel mark:%.2f\n",stud1.rollno,stud1.name,stud1.totalmark);
      printf("\nRoll number:%d\n\nName:%s\n\nTotel mark:%.2f\n",stud2.rollno,stud2.name,stud2.totalmark);
      printf("\nRoll number:%d\n\nName:%s\n\nTotel mark:%.2f\n",stud3.rollno,stud3.name,stud3.totalmark);
     getch();
     return 0;
     }

Array of structures:

                     It is possible to store a structure has an array element. i.e., an array in which each element is a structure. Just as arrays of any basic type of variable are allowed, so are arrays of a given type of structure. Although a structure contains many different types, the compiler never gets to know this information because it is hidden away inside a sealed structure capsule, so it can believe that all the elements in the array have the same type, even though that type is itself made up of lots of different types.

The declaration statement is given below.
struct struct_name
       {
       type element 1;
       type element 2;
       ……………..
       type element n;
       }array name[size];

Example:
struct student
       {
       int rollno;
       char name[25];
       float totalmark;
       } stud[100];

                          In this declaration stud is a 100-element array of structures. Hence, each element of stud is a separate structure of type student. An array of structure can be assigned initial values just as any other array. So the above structure can hold information of 100 students.

program:

#include  <stdio.h>
#include  <conio.h>
int main()
      {
      struct student
               {
               int rollno;
               char name[25];
               int totalmark;
               }stud[100];
      int n,i;
      clrscr();
      printf("Enter total number of students\n\n");
      scanf("%d",&n);
      for(i=0;i<n;i++)
                {
                printf("Enter details of %d-th student\n",i+1);
                printf("Name:\n");
                scanf("%s",&stud[i].name);
                printf("Roll number:\n");
                scanf("%d",&stud[i].rollno);
                printf("Total mark:\n");
                scanf("%d",&stud[i].totalmark);
                }
     printf("STUDENTS DETAILS:\n");
     for(i=0;i<n;i++)
               {
               printf("\nRoll number:%d\n",stud[i].rollno);
               printf("Name:%s\n",stud[i].name);
               printf("Totel mark:%d\n",stud[i].totalmark);
              }
    getch();
    return 0;
    }

Structure as structure member:

                              A structure can have one or more of its member as another structure, but a structure cannot be member to itself when a structure is used as structure member. In such situation, the declaration of the embedded structure must appear before the declaration of the outer structure. For example

#include  <stdio.h>
#include  <conio.h>
int main()
       {
       struct dob
                {
                int day;
                int month;
                int year;
                };
       struct student
                {
                struct dob d;
                int rollno;
                char name[25];
                int totalmark;
                }stud[25];
       int n,i;
       clrscr();
       printf("Enter total number of students\n\n");
       scanf("%d",&n);
       for(i=0;i<n;i++)
                {
               printf("\nEnter details of %d-th student\n\n",i+1);
               printf("\nName:\n");
               scanf("%s",&stud[i].name);
               printf("\nRoll number:\n");
               scanf("%d",&stud[i].rollno);
               printf("\nTotal mark:\n");
               scanf("%d",&stud[i].totalmark);
               printf("\nDate of birth (Format:01 06 2010):\n");
               scanf("%d%d%d",&stud[i].d.day,&stud[i].d.month,&stud[i].d.year);
               }
       printf("STUDENTS DETAILS:\n");
       for(i=0;i<n;i++)
               {
               printf("\n\nRoll number:%d\n\n",stud[i].rollno);
               printf("Name:%s\n\n",stud[i].name);
               printf("Totel mark:%d\n\n",stud[i].totalmark);
               printf("Date of birth:%d / %d / %d \n\n",stud[i].d.day,stud[i].d.month,stud[i].d.year);
               }
     getch();
     return 0;
}

External Storage class Home Union