Structure in C

Structure in C

  • Structure is the collection of variables of different types under a single name for better handling. 
For example: 
  • You want to store the information about person about his/her name, citizenship number and salary. 
  • You can create these information separately but, better approach will be collection of these information under single name because all these information are related to person.

Structure Definition in C
  • Keyword struct is used for creating a structure.
  • We can create the structure for a person as mentioned above as:
struct employee  
{   int id;  
    char name[50];  
    float salary;  
};  
  • Here, struct is the keyword, employee is the tag name of structure; idname and salary are the members or fields of the structure. Let's understand it by the diagram given below.
  • This declaration above creates the derived data type struct person.

Structure variable declaration

  • When a structure is defined, it creates a user-defined type but, no storage is allocated. For the above structure of person, variable can be declared as:

struct person
{
    char name[50];
    int cit_no;
    float salary;
};



Inside main function:
struct person p1, p2, p[20];

Another way of creating structure variable is

  • We can declare variable for the structure, so that we can access the member of structure easily. 
  • There are two ways to declare structure variable:
  1. By struct keyword within main() function
  2. By declaring variable at the time of defining structure.
1st way
Let's see the example to declare structure variable by struct keyword. It should be declared within the main function.
struct employee  
{   int id;  
    char name[50];  
    float salary;  
}; 

Now write given code inside the main() function.
struct employee e1, e2; 

2nd way
Let's see another way to declare variable at the time of defining structure.
struct employee  
{   int id;  
    char name[50];  
    float salary;  
}e1,e2; 

Which approach is good

  • But if no. of variable are not fixed, use 1st approach. It provides you flexibility to declare the structure variable many times.
  • If no. of variables are fixed, use 2nd approach. It saves your code to declare variable in main() fuction.
  • In both cases, 2 variables p1, p2 and array p having 20 elements of type struct person are created.

Understating Structure


Accessing members of a structure

  • There are two types of operators used for accessing members of a structure.
  1. Member operator(.)
  2. Structure pointer operator(->) (will be discussed in structure and pointers).

Any member of a structure can be accessed as


structure_variable_name.member_name

  • Suppose, we want to access salary for variable p2. Then, it can be accessed as

p2.salary
  • Structure in c language is a user defined datatype that allows you to hold different type of elements.
  • Each element of a structure is called a member.
  • It works like a template in C++ and class in Java. You can have different type of elements in it.
  • It is widely used to store student information, employee information, product information, book information etc.

/*Write a C program to add two distances entered by user. Measurement of distance should be in inch and feet.(Note: 12 inches = 1 foot)*/



#include <stdio.h>
struct Distance{
    int feet;
    float inch;
}d1,d2,sum;
int main()
{
    printf("1st distance\n");
    printf("Enter feet: ");
    scanf("%d",&d1.feet);  /* input of feet for structure variable d1 */
    printf("Enter inch: ");
    scanf("%f",&d1.inch);  /* input of inch for structure variable d1 */
    printf("2nd distance\n");
    printf("Enter feet: ");
    scanf("%d",&d2.feet);  /* input of feet for structure variable d2 */
    printf("Enter inch: ");
    scanf("%f",&d2.inch);  /* input of inch for structure variable d2 */
    sum.feet=d1.feet+d2.feet;
    sum.inch=d1.inch+d2.inch;
    if (sum.inch>12)
{  //If inch is greater than 12, changing it to feet.
        ++sum.feet;
        sum.inch=sum.inch-12;
    }
    printf("Sum of distances=%d\'-%.1f\"",sum.feet,sum.inch); 
/* printing sum of distance d1 and d2 */
    return 0;
}


Output:

1st distance
Enter feet: 12
Enter inch: 7.9
2nd distance
Enter feet: 2
Enter inch: 9.8
Sum of distances= 15'-5.7"

Keyword typedef while using structure

  • Programmer generally use typedef while using structure in C language. 
  • For example:
typedef struct complex
{
  int imag;
  float real;
}comp;


Inside main:
comp c1,c2;
  • Here, typedef keyword is used in creating a type comp(which is of type as struct complex). Then, two structure variables c1 and c2 are created by this comp type.

Structures within structures

  • Structures can be nested within other structures in C programming.

struct complex
{
 int imag_value;
 float real_value;
};
struct number{
   struct complex c1;
   int real;
}n1,n2;
  • Suppose you want to access imag_value for n2 structure variable then, structure member n1.c1.imag_value is used.

Example of structure in C language.

#include <stdio.h>  
#include <string.h>  
struct employee    
{   int id;    
    char name[50];    
}e1;  //declaring e1 variable for structure  
int main( )  
{  
   //store first employee information  
   e1.id=101;  
   strcpy(e1.name, "mskuthar");//copying string into char array  
   //printing first employee information  
   printf( "employee 1 id : %d\n", e1.id);  
   printf( "employee 1 name : %s\n", e1.name);  
   return 0;  
}
Output:
employee 1 id : 101
employee 1 name : mskuthar


Let's see another example of structure in C language to store many employees information.
#include <stdio.h>  
#include <string.h>  
struct employee    
{  int id;    
    char name[50];    
    float salary;    
}e1,e2;  //declaring e1 and e2 variables for structure  
int main( )  
{  
   //store first employee information  
   e1.id=101;  
   strcpy(e1.name, "mskuthar");//copying string into char array  
   e1.salary=56000;  
  //store second employee information  
   e2.id=102;  
   strcpy(e2.name, "James Bond");  
   e2.salary=126000;  
   //printing first employee information  
   printf( "employee 1 id : %d\n", e1.id);  
   printf( "employee 1 name : %s\n", e1.name);  
   printf( "employee 1 salary : %f\n", e1.salary);  
   //printing second employee information  
   printf( "employee 2 id : %d\n", e2.id);  
   printf( "employee 2 name : %s\n", e2.name);  
   printf( "employee 2 salary : %f\n", e2.salary);  
   return 0;  
}

Output:

employee 1 id : 101
employee 1 name : mskuthar
employee 1 salary : 56000.000000
employee 2 id : 102
employee 2 salary : 126000.000000
employee 2 name : James Bond


Post a Comment

Previous Post Next Post