Structure in C

Structure in C



What is Structure in C

Structure is basically a user-defined data type that can store logically related information of different data types.

Why do we need Structure ?

1. It is used to create a clubbed data type which contains different data types, like int, char, float etc.
2. It helps us to organize a complex data in a meaningful way.

Difference between Array and Structure :

Main difference is an Array can store only the information of same data type, in other hand a structure can store information of different data types.

How to create a Structure ?

Syntax :
struct structureName
  {
    datatype member1;
    datatype member2;
    ...
  };
Example :
  struct Student
  {
    char Name[50];
    char Grade;
    int Roll;
    float Marks;
  };
  //"struct" as keyword for structure
  //"student" as structure tag and name
  //Grade, Roll, Marks as member


What is member ?

Individual structure elements are called as “members”.

How to define Structure Variables ?

We can define structure variables in two way :
1st way :
struct Student
  {
    char Name[50];
    char Grade;
    int Roll;
    float Marks;
 };
 struct Student s1,s2;
2nd way :
  struct Student
  {
    char Name[50];
    char Grade;
    int Roll;
    float Marks;
  }s1,s2;

How to access members of Structure ?

We can access structure members in two ways :

1. Member or Dot Operator :
This operator used to access members of structure.
Eg. s1.Name
1st way :
 //Write a program in C to declare and print structure named student with variable student_name, roll, marks, grade.
 
 #include<stdio.h>
 int main()
 {
 struct student
 {
    char student_name[20];
    int roll;
    float marks;
    char grade[3];
 };
  /* struct student s1;
  s1.roll = 117; s1.marks = 56.47; */
  	struct student s1 = {"Arijit Roy",117,56.47,"B"};
    	printf("Given details are : ");
        printf("Name : %s\nRoll : %d\nMarks : %f\nGrade : %s\n",s1.student_name,s1.roll,s1.marks,s1.grade);
    return 0;
 }
2nd way :
 //Write a program in C to declare and print structure named student with variable student_name, roll, marks, grade.
  
 #include<stdio.h>
 #include<string.h>
 struct Student
 {
    char student_name[20];
    int roll;
    float marks;
    char grade[3];
  }s1;		//here we declare s1 variable
 int main()
 {
	strcpy(s1.student_name,"Arijit Roy"); //for copying string into char
	s1.roll=117;
	s1.marks=56;
	strcpy(s1.grade,"B"); //for copying string into char 
    	printf("Given details are : ");
        printf("Name : %s\nRoll : %d\nMarks : %f\nGrade : %s\n",s1.student_name,s1.roll,s1.marks,s1.grade);
    return 0;
  }

2. Structure Pointer Operator :
This operator used to access pointer in a structure.
Eg. P-->name
Example :
//Write a program in C to declare and print structure named Book with variables bookname, page number and price (using pointer)

#include<stdio.h>
int main()
{
	struct book
	{
		char name[50];
		int pageno;
		float price;
	};
	struct book b;
	printf("Enter the book name : \n");
	scanf("%s",b.name);
	printf("Enter the page no : \n");
        scanf("%d",&b.pageno);
	printf("Enter the book price : \n");
	scanf("%f",&b.price);
	struct book *p;
	p=&b;
	printf("Your Book Name is :%s \nTotal Page is :%d \nBook Price is :%.2f \n",p->name,p->pageno,p->price);
    return 0;
}
Output :



What is an Array of structures ?

By name, Array of structures means it is a contiguous memory location, that will store value of structure data type. So that array of structure also known as the collection of structure.

Sample Program :
//Write a program in C to declare a structure array of Book with variable book name, page number.
  
#include<stdio.h>
int main() 
{
struct book 
{
    char name[10];
    int page_no;
};
int i,size;
printf("Enter the number upto which you want to check : ");
scanf("%d",&size);
struct book b[size];		//using array 
for (i = 0; i < size; i++)
{
      printf ("Enter the book name: \n");
      scanf ("%s", b[i].name);
      printf ("Enter the page no: \n");
      scanf ("%d", &b[i].page_no);
}
for (i = 0; i < size; i++)
    printf ("The books name with page no are : %s %d \n", b[i].name, b[i].page_no);
return 0;
}
Output :

Limitations of C Structure :

1. Two structure variables can not be compared for equality or inequality.
2. C structures do not permit functions inside structure.