C Structure

Structure is commonly refer to as user-defined data type. Structure is similar to an array but the only difference is that array is collection of similar data type on the other hand structure is collection of different data type. A structure can contain any data type including array and another structure as well. Each variable declared inside structure is called member of structure.

How to create structure?

To create structure in c, we use the keyword called "struct". We use the following syntax to create structures in c programming language.

struct <structure_name>
{
      data_type member1;
      data_type member2, member3;
      .
      .
} ;

Following is the example of creating a structure called Student which is used to hold student record.

struct Student
{
    char stud_name[30];
    int roll_number;
    float percentage;
} ; 
Following is the example of creating a structure called Employee which is used to hold 
employee record.

struct employee  
{   int id;  
    char name[20];  
    float salary;  
}; 

The following image shows the memory allocation of the structure employee that is defined in the above example.


 Here, struct is the keyword; employee is the name of the structure; id, name, and salary are the members or fields of the structure. Let's understand it by the diagram given below:
 


Declaring structure variable

We can declare a variable for the structure so that we can access the member of the structure easily. There are two ways to declare structure variable:

  1. By struct keyword within main() function
  2. By declaring a variable at the time of defining the structure.

1st way:

Let's see the example to declare the 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;  

The variables e1 and e2 can be used to access the values stored in the structure.

2nd way:

Let's see another way to declare variable at the time of defining the structure.

     struct employee  
    {   int id;  
        char name[50];  
        float salary;  
    }e1,e2;  



Which approach is good

If number of variables are not fixed, use the 1st approach. It provides you the flexibility to declare the structure variable many times.

If no. of variables are fixed, use 2nd approach. It saves your code to declare a variable in main() function.

Accessing members of the structure

There are two ways to access structure members:

  1. By . (member or dot operator)
  2. By -> (structure pointer operator)

Let's see the code to access the id member of p1 variable by. (member) operator.

p1.id  

Example 1: Using (.) dot operator

#include<Stdio.h>

struct Student
{
    char stud_name[30];
    int roll_number;
    float percentage;
} stud_1 ;                  // while defining structure

void main(){
    struct Student stud_2;  // using struct keyword
    
    printf("Enter details of stud_1 : \n");
    printf("Name : ");
    scanf("%s", stud_1.stud_name);
    printf("Roll Number : ");
    scanf("%d", &stud_1.roll_number);
    printf("Percentage : ");
    scanf("%f", &stud_1.percentage);
    
    printf("\n *****  Student 1 Details *****\n");
    printf("Name of the Student : %s\n", stud_1.stud_name);
    printf("Roll Number of the Student : %i\n", stud_1.roll_number);
    printf("Percentage of the Student : %f\n", stud_1.percentage);

Output:

 

Example 2 : Using -> pointer operator

#include<Stdio.h>
struct Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;
       };

       void main()
       {

              struct Employee E;             //Statement  1
              struct Employee *record;
              record=&E;
                    printf("\nEnter Employee Id : ");
                    scanf("%d",&record->Age);

                    printf("\nEnter Employee Name : ");
                    scanf("%s",&record->Name);

                    printf("\nEnter Employee Age : ");
                    scanf("%d",&record->Age);

                    printf("\nEnter Employee Salary : ");
                    scanf("%ld",&record->Salary);

                    printf("\n\nEmployee Id : %d",record->Id);
                    printf("\nEmployee Name : %s",record->Name);
                    printf("\nEmployee Age : %d",record->Age);
                    printf("\nEmployee Salary : %ld",record->Salary);


       }

 

Output:


No comments:

Post a Comment