structure in c

The structure is a user-defined datatype in c language, which is the combination of the different datatypes. The structure is partially similar to an array.
The only difference between these two is, the array is the collection of similar data types, and structure is the collection of the different datatypes.
The structure makes the complex program simple, which can be easier to understand.
In a program, if there is a need of declaring more number of variables related to a particular topic then, it would be difficult for the programmer to declare different variables and access them separately. What if, all the variables combine under the single user-defined variable. The programmer's task will become easy and sorted. The programmer can access all the variables using a single user-defined variable. This will also reduce the complexity of the program.

 To understand the structure, let us take an example,
There is a program where we want to store the student's details such as name, roll_no, id_no, address.

 1. C program without using a structure.


#include<stdio.h>

int main()
{
char name[3][10];
int roll_no[3],i;
long int id_no[3];
char address[3][10];

printf("\n Enter the names of the students:");
for(i=0;i<3;i++)
scanf("%s", name[i]);


printf("\n Enter the roll_no of the students:");
for(i=0;i<3;i++)
scanf("\n %d",&roll_no[i]);


printf("\n Enter the id_no of the students:");
for(i=0;i<3;i++)
scanf("\n %ld",&id_no[i]);


printf("\n Enter the address of the students:");
for(i=0;i<3;i++)
scanf(" %s",address[i]);


printf("\n Print the name, roll-no,id_no, and address of the students:");


printf("\n\n Name:");
for(i=0;i<3;i++)
printf("\n%d. %s ",i+1, name[i]);


printf("\n roll_no:");
for(i=0;i<3;i++)
printf("\n%d. %d ",i+1,roll_no[i]);


printf("\n id_no:");
for(i=0;i<3;i++)
printf("\n%d. %ld ",i+1,id_no[i]);


printf("\n Address:");
for(i=0;i<3;i++)
printf("\n%d. %s ",i+1, address[i]);
return 0;
}

Output: 

Enter the names of the students:

Rahul Kartik Pooja
 Enter the roll_no of the students:

10 12 24
 Enter the id_no of the students:

1234 1235 1236
 Enter the address of the students:

Pune Mumbai Nashik

 Print the name, roll-no,id_no, and address of the students:

 Name:
1. Rahul
2. Kartik
3. Pooja
 roll_no:
1. 10
2. 12
3. 24
 id_no:
1. 1234
2. 1235
3. 1236
 Address:
1. Pune
2. Mumbai
3. Nashik
 

We have entered the details of the students. But what if the details were more than the present details, the declaration of variables would be more and the program would have become more complicated.
 What if we create a single user-defined variable that will consist of all the variables required in it.

 2. C Program using a structure.


#include<stdio.h>
int main()
{
struct student
{
  char name[10];
  int roll_no,i;
  long int id_no;
 char address[10];
};
struct student s1,s2,s3;
printf("Enter the details of the students: \n");
scanf("%s %d %ld %s", s1.name,&s1.roll_no, &s1.id_no,s1.address);
scanf("%s %d %ld %s", s2.name,&s2.roll_no, &s2.id_no,s2.address);
scanf("%s %d %ld %s", s3.name,&s3.roll_no, &s3.id_no,s3.address);
printf("\n    Name Roll_no Id_no Address");
printf("\n 1. %s  %d   %ld   %s", s1.name,s1.roll_no,s1.id_no,s1.address);
printf("\n 2. %s  %d   %ld   %s", s2.name,s2.roll_no,s2.id_no,s2.address);
printf("\n 3. %s  %d   %ld   %s", s3.name,s3.roll_no,s3.id_no,s3.address);
return 0;
}


Output: 
 
Enter the details of the students: 
Rahul 10 1234 Pune
Kartik 12 1235 Mumbai
Pooja 24 1236 Nashik

Name Roll_no Id_no Address
1. Rahul 10 1234 Pune
2. Kartik 12 1235 Mumbai
3. Pooja 24 1236 Nashik 

student= student is the structure name or structure data type

s1,s2,s3= these are variable name of structure data type “student”.
As you see, there is a huge difference between both programs.
In the first program, every student variable is initialized and accessed differently, and the complexity of the program is also increased.
In the second program, A single user-defined structure of student datatype, which combines all the different datatype is created. The user-defined variables of the student datatype are declared. The user-defined variables s1, s2, s3 of student datatype consist of the combination of all the different datatype defined in the structure data type.
This makes the program sorted and easy to understand with less complexity.

There are different syntax for structure.

structure-c

structure-c

structure-c

structure-c

structure-c

structure-c

Note: the sequence in which the variables are declared inside the structure, in the same sequence they should be initialized.


  • In structure, the entire structure variable declared can be initialized to zero.

This is one benefit of structure that we do not need to initialize the variables defined under it each separately.

Eg., struct student stud={0};   

  • By defining the structure, the memory does not get allocated. When we declare the structure variable at that time, the memory gets allocated for the structure variable.

How to access the structure elements?

If we want to access the elements of the structure, we use the (.) operator.


#include<stdio.h>

int main()
{
 struct company
{
  char name[10];
  int employes;
  char place[10];
};
struct company c={"TCS", 5000,"pune"};

printf("name=%s address=%u\n",c.name,&c.name);
printf("employes=%d address=%u\n",c.employes,&c.employes);
printf("place=%s address=%u\n",c.place,&c.place);
return 0;
}

 output:

structure-c



structure-c