Structure in C programming7 min read

  • A structure in C is defined as a collection of one or more variables possibly of different data types called heterogeneous types grouped together under a single name for convenient handling.
  • Structure helps to organize complicated data, particularly in large programs as they permit a group of related variables to be treated as a unit instead of separate entities.
  • They are especially useful in file handling to read the exact amount of data from disk and write back to disk with one instruction.

Structure Declaration

Syntax :

storage class struct tag

{

  data type member_1_name; 
  data_type member_2_name;
  .......................; 
  data_type member_n_name; 

} [Structure_variable];

Here, one may define the structure variable during the declaration of structure or at a later stage. When declaring all the structure variables during declaration then defining the structure tag (which helps you to declare a variable of a structure type) is optional.

Example 1)

struct admission

{ 

  char name[10]; 
  int age; 
  char status; 

};
Storage of a structure in C

The parts of the structure name[20], age, and status are called structure members or elements. Here the total size of the structure is 13 bytes (10 for name + 2 for age + 1 for status). One may use sizeof the operator to find the size.

int size = sizeof(admission);

Example 2)

struct 

{

  char name[10];
  int age;
  char status; 

}s;

Here, s is a variable of structure type. The structure tag is missing and is optional as we have declared the structure variable during the definition.

Period Operator

The members of a structure are usually processed individually as separate entities. Therefore, we must be able to access the individual structure members. A structure member can be accessed by writing

Structure_variable_name.Member_name For example suppose structure is declared as follows:

The period (.) separates the structure variable name from the member name. The period is the structure member operator, which is generally referred to as a period operator or member operator, or dot operator. Here structure names are called qualifiers or designators to their elements. This period is an operator, it is a member of the highest precedence group and its associativity is left-to-right.

struct date 

{

  int day; 
  int month; 
  int year; 

};

struct date today;

Here variable today is also a structure of date type. Now a member of a structure is accessed by specifying the structure variable name followed by a period or dot and then the individual member name

  • For example to set the new date 20-6-2016 in the structure variable name today:
    • today.day = 20;
    • today.month = 6;
    • today.year = 2016;

Note that there are no spaces permitted between the variable names, the period, and the member name. In short, the link between a variable and a member is established using the operator. (dot or period).

Structure Initialization

  • Like any other data type, a structure variable can be initialized.
  • The general form for initializing the structure is to place an equal sign after structure declaration, followed by the values inside a pair of braces. Individual values within pair of braces are separated by a comma.
  • While initialization, the order of values must be in accordance with the order in which the member is specified.

Example ( Struct admission, now structure variable initialization is shown below )

void main (void)

{

  struct admission
  
  {
  
    char name [20];
    int age; 
    char sex;
    
  }sl={"Chitra Singh".18,'f'};
  
  printf("%s", sl.name); 
  printf(" %d %c", sl.age, sl.sex);
    
}

Giving Values to Member

We can assign value to the members of the structure individually.

Example (Assigning values to member)

void main(void)

{

  struct admission 
  
  {

    char name[20];
    int age ;
    char status; 
    
  }; 
  
  struct admission admi;    /* Structure Variable declaration */ 
  strepy( admi.name , "Akhil" ); 
  admi.age = 14; 
  admi.status = 'm'; 
  printf("%s %d %c", admi.name , admi.age, admi.status);
  
}

Comparison of Structure Variable

Members of two variables of the same structure type can be compared in the same way as ordinary variables.

Example ( Comparing structure variable )

void main(void) 

{

  struct class 
  
  {
    
    int rollno; 
    char name[20];
    float avg:
    
  };
  
  inte;
  
  struct class student1 = {101, "sai", 77.50}; 
  struct class student2 = {102, "baba", 89.90}; 
  struct class temp; 
  
  temp = student2;      /* Copy the contents, to temp variable */ 
  e=((temp. rollno == student2.rollno) && (temp.avg==student2.avg))?1:0; 
  
  if (e= =1)
  
    printf("\n Values are copied successfully"); 
    
  else
    
    printf("\n Values differ ");
    
}

You can copy the members of one structure variable to those of another structure variables as long as both structure variables are of the same type.

Note

Arrays of Structure

As we studied earlier, an array is a collection of entities that belongs to the same data type. The entities can also be structured and known as array of structure. For example, consider the following structure,

struct date 

{

  int day; 
  int month; 
  int year;

} dob;
  • With date structure, it is only necessary to keep track of one variable instead of three. Suppose we require keeping the birth dates of 40 students in a class.
  • A better method for handling 40 different birth dates involves the combination of two powerful features of the ‘C’ programming language: Structures and Array i.e., Array of Structure.
  • Here each element of the array can be structure of a similar data type.
  • For the above data structure, the following declaration is made.

struct date dob[40];

  • Defines an array called dob which can store the date of birth of 40 students.
  • Each element inside the array is defined to be a structure of type struct date.
  • Now if you want to print the year when 4″ student date of birth falls following printf statement will achieve the same.

printf(“%d”,dob[3].year);

void main(void) {

Example

Define the structure having members rollno, name, and marks of 3 papers say p,c,m. Accept the information of 20 students and print them along with the total marks

void main(void)

{

  struct stud 
  
  {

    int rollno; 
    char name[20];
    int p,c,m; }; 
    
    struct stud c[20]; 
    int i, total; 
    
    clrscr(); 
    
    for( i = 0; i <20; i++) 
    
    {
    
      printf("Enter rollno, name, and marks of Physics, Chemistry, and Maths of%d student", ;+1); 
      scanf("%d%5%d%d%d", &c.[i].rollno.c[i].name, &c[i].p. &c[i].c, &c[i].m);
      
    } 
    
    fort i 0:1-20; i++)

    {
    
      total (clil.p + cfil.c + cfilm);
      printf(" n%d %s %ed%d %d %f", c[i].rollno, c[i].name, c[i].p, c[i].c, c[i].m, total); 
      
    }
    
}
  • Here. 1) struct stud c[20]; provides the space in memory for 20 structures of the type struct stud
  • 2) If we have not used arrays in the above problem then one has to define lot of variables.

Nested Structure

  • ‘C’ provides the user with an enormous amount of flexibility in defining structure.
  • For instance, a structure can be defined as an element of another structure and is known as nesting of structure the structure to be included as part of another structure can be defined inside the structure definition or outside as an individual structure type.
  • This is called nesting or embedded structure or structure within structure.

Let us consider the following structure defined to store information about the marks scored by the students in the exam.

struct exam

{

  int rollno; 
  char name[20]; 
  int phy; 
  int chem; 
  int math;

}s;

This structure defines rollno, name, and marks of 3 papers. We can group all the items related to marks of paper together and declare them under a sub-structure as shown below,

Structure inside definition

struct exam

{ 

  int rollno;
  char name[20]; 
  struct 

  {
    

    int phy; 
    int chem; 
    int math;

  }p;

}s;

Structure outside definition

struct exam

{ 

  int phy; 
  int chem; 
  int math;

};

  struct exam; 
 
  {
   
    int rollno; 
    char name[20]; 
    struct mark p;

}s;

Now the paper math is referred to as s.p.math;

An innermost member is a nested structure that can be accessed by chaining the entire concerned structure variable (from outermost to innermost) with the member using the dot operator. i.e. to refer or print math mark, use following case printf(“math mark = %d”. s.p. Math);


Share:

Leave a Reply