C Structures
In this tutorial, we will be covering Structures in C Programming Language in detail.
The structure is a user-defined data type in C language which allows us to combine data of different types together. Structure helps to construct a complex data type that is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only. While structure, on the other hand, can store data of any type, which is practical more useful.
For example:
If I have to write a program to store Student information, which will have Student's name, age, branch, permanent address, father's name, etc, which included string values, integer values, etc, how can I use arrays for this problem, I will require something which can hold data of different types together.
In structure, data is stored in form of records. Thus a structure is a collection of data items of different data types which are logically combined into a single unit.
Defining a Structure
As we know that structure is composed of data items that may be of different data types. The data items of a structure are mainly known as Fields,where each field has an identifier that is field name and a data type.struct keyword is used to define a structure. struct defines a new data type which is a collection of primary and derived data types.
Syntax of Structure in C
The following is the syntax of Structure in C.
struct [structure_tag]
{
//member variable 1
//member variable 2
//member variable 3
...
}[structure_variables];
As you can see in the syntax above, we start with the struct keyword, then it's optional to provide your structure a name, we suggest you to give it a name, then inside the curly braces, we have to mention all the member variables, which are nothing but normal C language variables of different types like int, float, array etc.
After the closing curly brace, we can specify one or more structure variables, again this is optional.
Note: The closing curly brace in the structure type declaration must be followed by a semicolon(;).
Example of Structure in C
let's create an example to understand the structure in C.
struct Student
{
char name[25];
int age;
char branch[10];
// F for female and M for male
char gender;
};
Here struct Student declares a structure to hold the details of a student which consists of 4 data fields, namely name, age, branch and gender. These fields are called structure elements or members.
Each member can have different datatype, like in this case, name is an array of char type and age is of int type etc. Student is the name of the structure and is called as the structure tag.
Declaring Structure Variables or reference
It is possible to declare variables of a structure, either along with structure definition or after the structure is defined. Structure variable declaration is similar to the declaration of any normal variable of any other datatype. Structure variables can be declared in the following two ways:
1) Declaring Structure variables separately
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
};
struct Student S1, S2; //declaring variables of struct Student
2) Declaring Structure variables with structure definition
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
}S1, S2;
Here S1 and S2 are variables of structure Student. However, this approach is not much recommended.
Accessing Structure Members in C
Structure members can be accessed and assigned values in a number of ways. In order to assign a value to any structure member, the member name must be linked with the structure variable using a dot . operator also called period or member access operator.
#include<stdio.h>
#include<string.h>
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
};
int main()
{
struct Student s1;
/*
s1 is a variable of Student type and
age is a member of Student
*/
s1.age = 18;
/*
using string function to add name
*/
strcpy(s1.name, "Viraaj");
/*
displaying the stored values
*/
printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);
return 0;
}
Name of Student 1: Viraaj Age of Student 1: 18
We can also use scanf() to give values to structure members through the terminal.
scanf(" %s ", s1.name);
scanf(" %d ", &s1.age);
Structure Initialization
Like a variable of any other data type, the structure variable can also be initialized at compile time.
struct Patient
{
float height;
int weight;
int age;
};
struct Patient p1 = { 180.75 , 73, 23 }; //initialization
or,
struct Patient p1;
p1.height = 180.75; //initialization of each member separately
p1.weight = 73;
p1.age = 23;
Array of Structure in C
We can also declare an array of structure variables. in which each element of the array will represent a structure variable. Example : struct employee emp[5];
The below program defines an array emp of size 5. Each element of the array emp is of type Employee.
#include<stdio.h>
struct student
{
int roll_no;
char name[30];
int marks;
};
int main()
{
struct student stud[2];
int i;
for(i=0; i<=2; i++)
{
printf("Student %d\n",i+1);
printf("Enter roll no. :\n");
scanf("%d", &stud[i].roll_no);
printf("Enter name :\n");
scanf("%s",stud[i].name);
printf("Enter marks :\n");
scanf("%d", &stud[i].marks);
}
for(i=0; i<=2; i++)
{
printf("Student %d\n",i+1);
printf("Roll no. : %d\n", stud[i].roll_no);
printf("Name : %s\n", stud[i].name);
printf("Marks : %d\n", stud[i].marks);
}
return 0;
}
Student 1
Enter roll no. :
1
Enter name :
Ajay
Enter marks :
56
Student 2
Enter roll no. :
2
Enter name :
kunal
Enter marks :
89
Student 3
Enter roll no. :
4
Enter name :
rohit
Enter marks :
56
Student 1
Roll no. : 1
Name : Ajay
Marks : 56
Student 2
Roll no. : 2
Name : kunal
Marks : 89
Student 3
Roll no. : 4
Name : rohit
Marks : 56
Nested Structures in C
Nesting of structures is also permitted in the C language. Nested structures mean, that one structure has another structure as a member variable. See the example below.
struct Student
{
char[30] name;
int age;
/* here Address is a structure */
struct Address
{
char[50] locality;
char[50] city;
int pincode;
}addr;
};
Structure as Function Arguments
We can pass a structure as a function argument just like we pass any other variable or an array as a function argument. See the example below.
#include<stdio.h>
struct Student
{
char name[10];
int roll;
};
void show(struct Student st);
void main()
{
struct Student std;
printf("\nEnter Student record:\n");
printf("\nStudent name:\t");
scanf("%s", std.name);
printf("\nEnter Student rollno.:\t");
scanf("%d", &std.roll);
show(std);
}
void show(struct Student st)
{
printf("\nstudent name is %s", st.name);
printf("\nroll is %d", st.roll);
}
Enter Student record:
Student name: kunal
Enter Student rollno.: 12
student name is kunal
roll is 12
Frequently Asked Questions(FAQ)
Ques1: What do you understand by the structure in C?
The structure is a user-defined data type in C language that allows us to combine data of different types together. Structure helps to construct a complex data type that is more meaningful.
Ques 2: Which keyword is mainly used to define the new structure?
The struct keyword is mainly used to define the new structure in C.
Ques 3: Which operator is used to access the structure elements using the structure variable?
The dot(.) operator is mainly used to access the elements of the structure using the structure variables.
Conclusion
This tutorial was all about structures in C. We have covered how to define the structure in C. We have covered structure variables and also covered how to access the members of the structure. We have also covered how a structure can be passed as an argument to the function.