Structure In C Language – CsTutorialpoint
Hello friends, in today’s article we are going to talk about Structure In C Language.
So now without wasting time let’s first understand what is the structure in c language.
What is Structure in C
In C language, when we want to group variables of different data types, then we use Structure. Structure is a user-defined data type with the help of which we can create a group of variables of different data types.
Through structure, we can create a new data type and all the elements inside this new data type are called members.
Structure is like an array, but one of the main differences between Array and Structure is that with the help of an array we can create a group of variables of the same data type, and with the help of structure we can create a group of variables of the different data types.
How to Create a Structure in C
In C language, if we want to create a new data type with the help of Structure, then for this we have to use the “struct” keyword.
Its basic syntax is something like this -:
Syntax -:
struct struct_name
{
Data_Type member1_name;
Data_Type member2_name;
Data_Type member3_name;
…
};
- struct is a keyword with the help of which we create a new data type
- struct_name – This is the name of the structure data type, you can keep this name as per your choice like – student, college, class, etc.
- Data_Type – Data_Type can contain int, char, float, double anything.
- member1_name – This is the name of the variable. Every element located inside the structure is called a member.
Example -:
struct student
{ int roll_no;
char name[20];
float percent;
};
Here struct is the keyword and student is a new user-defined data type inside which three Structure member variables named roll_no, name, and percent are declared with the help of int, char, and float.
If we want to access these three members, then for this we have to declare a variable of this student data type.
How to Declare Structure Variables In C
We declare Structure variables so that Structure members can be easily accessed and used anywhere in our program.
Example -:
struct employee
{ int id;
char name[50];
float salary;
};
Here struct is the keyword and employee is a new user-defined data type inside which three structure member variables are declared with the help of int, char, and float, named id, name, and salary. If we want to access these three members, then for this we have to declare a variable of this employee data type.
In C language we can declare structure variable in two ways -:
- By using the struct keyword inside the main() function.
- At the end of the structure when defining structure.
Method 1 -: By using the struct keyword inside the main() function
Let’s understand this better through an example -:
Example -:
#include<stdio.h>
struct employee
{ int id;
char name[50];
float salary;
};
void main()
{
struct employee e1, e2;
}
As I have mentioned above, to access the Structure Member, we have to declare the structure variable. So here in this example, we have declared two structure variables named e1, and e2 with the help of which we can easily access the structure member.
There are some methods to access Structure Member through these structure variables, about which we will read further. For now, we’ll go through another method for declaring a structure variable.
Method 2 -: At the end of the structure while defining the structure
Let’s understand this also through an example -:
Example -:
struct employee
{ int id;
char name[50];
float salary;
} e1,e2;
Which Method is Good?
Of those two methods of declaring a structure variable, you would be right to use the first method when you do not know how many variables of that structure you have to declare in a program.
In the first method, you can declare the structure variable as many times as you need in your program. You have to do this at the time of declaring the variable separately inside the main() function.
If the number of structure variables is Fix then you should use another method.
Note – When we define a structure, then that structure does not get any place in the memory. Structure gets space in memory when we declare the variable of that structure.
Let us now understand how to initialize the structure member
How To Initialize Structure Members In C
We cannot initialize structure member with declaration, if we try to do so then compiler will give an error and program will not run.
struct emp
{
int a = 0; // COMPILER ERROR: cannot initialize members here
int b = 0; // COMPILER ERROR: cannot initialize members here
};
The reason for the error is that whenever we define the structure, the structure does not get any memory. Structure gets memory when we create structure variables.
We can initialize Structure members in these ways -:
Method 1 -:
#include<stdio.h>
struct student
{
char name[50];
int roll_no;
int age;
};
void main()
{
struct student s1 = { "Jeetu" , 73, 21 }; //initialization
}
Method 2: Using Dot(.) Operator
#include<stdio.h>
struct student
{
char name[50];
int roll_no;
int age;
};
void main()
{
struct student s1;
//initialization of each member separately
strcpy(s1.name, "Jeetu") ; //copying string into char array
s1.roll_no = 73;
s1.age = 21;
}
Note -: We access the structure members by dot (.) operator and initialize the value in them.
How To Access Structure Members
We can access the structure member in many ways. Let us know about those methods.
- by Dot(.) (member or dot operator)
- by -> (structure pointer operator)
Method 1 -: by Dot(.) operator
Through the dot(.) operator, we can access the structure variables in this way and can also print them by initializing any value in them.
Examples -:
Examples -:
#include<stdio.h>
struct student
{
char name[50];
int roll_no;
int age;
};
void main()
{
struct student s1;
//initialization of each member separately
strcpy(s1.name, "Jeetu") ; //copying string into char array
s1.roll_no = 73;
s1.age = 21;
printf( "student name %s, roll no %d and age = ", s1.name, s1.roll_no, s1.age);
}
Output -:
student name Jeetu, roll no 73, and age = 21
Method 2: by -> (structure pointer operator)
We will cover how to access structure variables by -> (structure pointer operator) in our pointer to structure topic. for now let’s understand about nested structure
Nested Structure
In C language, we can declare another structure as a member variable inside the structure and when we do this, then it is called Nested Structure.
Example -:
struct Student
{
char[50] name;
int age;
/* here Address is a structure */
struct Address
{
char[50] locality;
char[50] city;
int pincode;
}addr;
};
Friends, till now you must have understood what is structure in c language, but do you know why we use structure in C language? if not! then let’s understand
Why do we need Structure?
There are some pre-defined data types in C language, with the help of which we can store only one information related to an object at a time.
If we want to store any object like student roll number, name, age, percent, etc which is different type of data together then we cannot do this with the help of predefined data type so we need structure.
We can store any type of data like int, char, float, double, etc. in the new data type that we create through the structure.
Advantages of Structure in C
- Through structure, we can store different types of data values together.
- It is quite easy to maintain because, in this, the entire record can be represented by a single name.
- Through struct, we can easily pass a complete record to the function.
- If you want to store multiple records of the same type, you can create an array of structures.
Disadvantages of Structure In C
- When an IT project becomes very complex, it is difficult to manage
- Structure is slow because it needs to store a lot of data.
- You can retrieve one struct member at a time.
Example Program of Structure in C
#include<stdio.h>
struct book
{ char title[50];
float price ;
int id;
};
void main()
{
struct book b1;
printf("Enter Book Title , price and book id\n");
gets(b1.title);
scanf("%f", &b1.price);
scanf("%d",&b1.id);
printf("\nBook Title, price and book id is : \n %s \n %f \n %d",b1.title,b1.price, b1.id);
}
Output -:
Enter Book Title , price and book id
Structure in C
599.5
101
Book Title, price and book id is :
Structure in C
599.5
101
Read More
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, I hope that after reading this article you know very well, what is Structure in C language.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
If you liked this post, then do not forget to share this post with your friends so that they can also get information about Structure in C Language
If you still have any questions or doubts related to Structure in C, then you must tell me, I will answer all your questions and you can contact us for more information.
Subscribe to this website newsletter to get information related to this new technology, Programming Language, Coding, C Language, C++, Python Course, Java Tutorial.