What is structure?

A structure is a user-define data type in C/C++. Structure creates data type that can be used to group items of different types into a single type. Define a structure you must use struct keyword. Each element of a structure is called a member.

Let’s see the syntax to define the structure in C

struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};

Let’s see the example of structure in C

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

Why use Structure?

Structure is user-define datatype which allows us to combine data of different types together. It’s a very similar to an array but an array holds data of similar type only and it can have different attributes of different data types. To store such type of information regarding an entity student. Use a special data structure to store the collections of different data types.

Let’s see the example

#include<stdio.h>  
void main ()  
{  
  char names[2][10],dummy;s   
  int roll_numbers[2],i;  
  float marks[2];  
  for (i=0;i<3;i++)  
  {  
      
    printf("Enter the name, roll number, and marks of the student %d",i+1);  
    scanf("%s %d %f",&names[i],&roll_numbers[i],&marks[i]);  
    scanf("%c",&dummy); 
  }  
  printf("Printing the Student details ...\n");  
  for (i=0;i<3;i++)  
  {  
    printf("%s %d %f\n",names[i],roll_numbers[i],marks[i]);  
  }  
}

Output

next →← prev
C Structure

In C, there are cases where we need to store multiple attributes of an entity. It is not necessary that an entity has all the information of one type only. It can have different attributes of different data types. For example, an entity Student may have its name (string), roll number (int), marks (float). To store such type of information regarding an entity student, we have the following approaches:

Construct individual arrays for storing names, roll numbers, and marks.
Use a special data structure to store the collection of different data types.

Let’s look at the first approach in detail.

#include<stdio.h>  
void main ()  
{  
  char names[2][10],dummy; // 2-dimensioanal character array names is used to store the names of the students   
  int roll_numbers[2],i;  
  float marks[2];  
  for (i=0;i<3;i++)  
  {  
      
    printf("Enter the name, roll number, and marks of the student %d",i+1);  
    scanf("%s %d %f",&names[i],&roll_numbers[i],&marks[i]);  
    scanf("%c",&dummy); // enter will be stored into dummy character at each iteration  
  }  
  printf("Printing the Student details ...\n");  
  for (i=0;i<3;i++)  
  {  
    printf("%s %d %f\n",names[i],roll_numbers[i],marks[i]);  
  }  
}

Output

Enter the name, roll number, and marks of the student 1Ashi 90 91        
Enter the name, roll number, and marks of the student 2Ayushman 91 56      
Enter the name, roll number, and marks of the student 3Hait 89 69

Printing the Student details...
Ashi 90 91.000000                                                                      
Ayushman 91 56.000000  
Hait 89 69.000000

Declaring structure variable

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

  • By struct keyword within main() function
  • By declaring variable at the time of define the structure.

Let’s see the example to declare the structure variable by struct keyword and it should be declare within the main function.

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

Now write the given code inside the main() function –

struct employee e1, e2;

Let’s see the another way to declare variable at the time of define the structure.

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

Accessing members of the structure

There are two ways to access structure members

  • member or dot operator
  • structure pointer operator

Let’s see the example of structure

#include<stdio.h>  
#include <string.h>    
struct employee      
{   int id;      
    char name[50];      
    float salary;      
}e1,e2;     
int main( )    
{    
    
   e1.id=101;    
   strcpy(e1.name, "Neha Jaiswal"); 
   e1.salary=56000;       
   e2.id=102;    
   strcpy(e2.name, "Tanmay jain");    
   e2.salary=126000;        
   printf( "employee 1 id : %d\n", e1.id);    
   printf( "employee 1 name : %s\n", e1.name);    
   printf( "employee 1 salary : %f\n", e1.salary);      
   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 : Neha Jaiswal
employee 1 salary : 56000.000000
employee 2 id : 102
employee 2 name : Tanmay jain
employee 2 salary : 126000.000000

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews