Structures in C Language
Introduction to structure
A structure is a very user friendly data type which lets the user define the types of different data which can be stored in the structure, i.e we can store integer, float, character, and string in one place(under one identifier). The elements saved in a structure are called it’s members. It is widely used to store student information like name, age, marks and other such data in one structure. Structure is the same as an array. The difference is that, In array we can store same type of data but in structure we can store different type of data.
Structure
Once you create a structure, then it becomes a data type. Now you can create any variables of this data type. And you can also create an array of this data type. But when you initialize the value of this variable, you have to initialize the value of all the variables defined in that structure.
We can define the structure before the main method.
Defining the Structure
Struct keyword is used to define the structure. After this keyword the structure is given the unique name. After this, variables are created in curly braces and semicolon is applied after the ending curly bracket.
struct struct_Name { Structure member 1; Structure member 2; ... Structure member N; };
Let’s take an example :
Here, the name of the structure is taken in the name “Student” and there are four members of the structure,which have two data types. An integer for a ‘stu_id’ and ‘stu_age’ and character for a ‘stu_name’ and ‘stu_branch’.
struct Student { int stu_id; char stu_name[10]; int stu_age; char stu_branch; };
Structure Variable Declaration
Structure variables can be declared in two ways:-
- Structure variable is written even when the definition of structure is written.
- The variable of the structure is also done inside the main() function.
Syntax for Structure variable outside of structure definition
struct structure_name { data_type member 1; data_type member 2; ...... data_type member n; }structure_variable(s);
Example:
struct Student { int stu_id; int stu_age; char stu_name[10]; char stu_branch[10]; }info;
Syntax for structure variable in main() function.
struct structure_name { data_type member 1; data_type member 2; ...... data_type member n; }structure_variable(s); int main() { struct structure_name structure_variable_name; }
Example
struct Student { int stu_id; int stu_age; char stu_name[10]; char stu_branch[10]; }; int main() { struct Student info; }
Accessing Structure members
It can be assigned to values in many ways. Without the structure the members of the structure have no personal meaning. To provide a value to a member of any structure, the member name must be connected with the structure variables using dot(.). The operator is also called the term or member access operator.
Syntax for Accessing Structure Members
structure_variable_name.member_of _structure =value(optional);
Program 1:
#include<stdio.h> #include<conio.h> struct Student { char stu_name[10]; int stu_age; char stu_branch; int stu_id; }; int main() { struct Student s1; // s1 is a variable of Student type and s1. stu_age=17; //age is a member of student strcpy(s1.stu_name,"Somya"); // using string function to add name printf("Name of student 1:%s\n", s1. stu_name); printf("Age of student 1:%d\n", s1.stu_age); return 0; }
Output :
Name of student 1:Somya Age of student 1:17
Structure Initialization
- The structure initialization is of two type.
- we can also initialize the structure variable at compile time.
TYPE 1:-
struct Student { float height; int weight; int age; }; struct Student s1 = { 180.75 , 73, 23 }; //initialization
TYPE 2:-
struct Student s1; s1.height = 182.5 ; //initialization of each member separately s1.weight = 65; s1.age = 20;
Structure working with size(sizeof)
The member of the structure allocates different memory and the size of the complete structure is as its members.
Program 2
#include<stdio.h> #include<conio.h> struct Student { int stu_id; char stu_name[10]; }; int main() { struct Student info; printf("size of Student id is: %d bytes\n",sizeof(info.stu_id)); //size of stu_id printf("size of Student name: %d bytes\n",sizeof(info.stu_name)); //size of stu_name printf("size of Student structure: %d bytes",sizeof(info)); //size of Student return 0; }
Output :
size of Student id is: 4 bytes size of Student name: 10 bytes size of Student structure: 16 bytes
Array of Structure
We can create an array of structure like other primitive data types. In which each element of array will represent a structure variable.
Example
struct student stu[5];
Program 3
#include<stdio.h> struct Student { char name[10]; int id; }; struct Student stu[5]; int i, j; void ask () { for (i = 0; i < 3; i++) { printf ("enter %d Student record:\n", i + 1); printf ("Student name: \t"); scanf ("%s", stu[i].name); printf ("\nenter id:\t"); scanf ("%d", &stu[i].id); } printf ("\ndisplaying Student record:"); for (i = 0; i < 3; i++) { printf ("Student name is %s", stu[i].name); printf ("id is %d", stu[i].id); } } void main () { ask (); }
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment