Structure:
- A structure is a user-defined data type that holds data of different types, unlike arrays which can hold data of the same type.
- In the case of structure, the variables are declared not defined that is memory is not allocated.
- Memory will be allocated once we create an object for that structure.
- The size of the structure is the addition of all its members.
Example:
If a program needs to be written to store a student, it will have the student’s name, age, branch, marks, which include integer values, marks as a float, name as a string. Thus array cannot be used as it can hold data of the same type. Hence the best suited is the user-defined data type called structure which can hold data of different types.
struct student
{
int roll;
char *name;
char branch[10];
float marks;
};
Here the student is the name of the structure and is also known as the structure tag.
Please refer to the function in C for a better understanding of the section below.
Declaring the structure variable:
It is possible to declare a variable of a structure or to create an object for the structure. Members of the structure are not allocated memory until the object is created.
The structure variable declaration is similar to the declaration of any normal variable of any other data type. It is declared in two ways:
- Declaring the structure variable separately.
- Declaring Structure variable with structural definition.
Declaring the structure variable separately:
struct Student
{
int roll;
char *name;
char branch[10];
float marks;
};
struct Student stu1, stu2; // Declaring variables of struct Student
Declaring Structure variable with structural definition:
struct Student
{
int roll;
char *name;
char branch[10];
float marks;
}stu1,stu2;
Ideally, it’s always good practice to have a structure name that starts with Uppercase but is not mandatory.
Accessing Structure Members:
Structure members can be accessed and assigned values using two special operators. They are:
- dot . operator also called period or member access operator.
- Arrow -> operator
Example:
Using a dot operator:
#include <stdio.h>
#include <string.h>
struct Student
{
int roll;
char name[11];
char branch[10];
float marks;
};
int main()
{
struct Student stud1;
stud1.roll= 4;
strcpy (stud1.name, "techaccess");
strcpy (stud1.branch, "CS");
stud1.marks = 70.6;
printf ("\n The name of the student is: %s", stud1.name);
printf ("\n The marks of the student is %f\n", stud1.marks);
return 0;
}
Output:
The name of the student is: techaccess
The marks of the student is 70.599998
Using the arrow operator:
This is used when we have declared a variable which is a pointer to structure. It will be clear if pointer article is read before.
#include <stdio.h>
#include <string.h>
struct Student
{
int roll;
char name[10];
char branch[10];
float marks;
};
int main()
{
struct Student *student1= (struct student *) malloc(sizeof(struct Student));
student1->roll= 4;
strcpy (student1->name,"techaccess");
strcpy (student1->branch,"CS");
student1->marks =5.8;
printf ("\n The name of the student is: %s", student1->name);
printf ("\n The marks of the student is %f\n", student1->marks);
return 0;
}
Output:
The name of the student is: techaccess
The marks of the student is 70.599998
Structure Initialization:
Like any other variable of any other data type, the structure variable can also be initialized at compile time.
struct Student
{
int roll;
char name[10];
char branch[10];
float marks;
};
struct Student stud1 = { 4, "techaccess", "CS", 79.8};
OR
stud1.roll= 4;
strcpy (stud1.name, "techaccess");
strcpy (stud1.branch, "CS");
stud1.marks = 70.6;
Array of Structure:
We can also declare an array of structure variables where each element of an array is a structure in itself.
Example: struct students[5];
This can be useful in case we have to store the information of each student in a class.
Example:
#include <stdio.h>
#include <string.h>
struct Student
{
char name[10];
int roll;
float marks;
};
int main ()
{
struct Student stud[3];
strcpy (stud[0].name, "tech");
stud[0].roll =5;
stud[0].marks = 5.6;
strcpy (stud[1].name, "access");
stud[1].roll =6;
stud[1].marks = 8.0;
printf("\n The student details are:\n");
int i;
for ( i =0; i<2; i++)
{
printf ("\n The name of student is: %s\n", stud[i].name);
printf ("\n The roll number is: %d\n", stud[i].roll);
printf ("\n The marks is: %f\n", stud[i].marks);
printf("\n");
}
return 0;
}
Output:
The student details are:
The name of student is: tech
The roll number is: 5
The marks is: 5.600000
The name of student is: access
The roll number is: 6
The marks is: 8.000000
Nested Structure:
Nested structure is also permissible in C, meaning one structure can have another structure.
Example:
#include <stdio.h>
#include <string.h>
struct Student
{
char name[10];
int roll;
struct Address
{
char address[10];
int pin;
}addr;
};
int main ()
{
struct Student stud;
strcpy (stud.name,"tech");
stud.roll = 6;
strcpy(stud.addr.address, "ABC");
stud.addr.pin =10;
return 0;
}
Passing structure to a function:
The structure can be passed as a parameter to a function:
#include <stdio.h>
#include <string.h>
struct Student
{
char name[10];
int roll;
};
void fun (struct Student *stud)
{
printf ("\n The student name is: %s and roll number is: %d\n", stud->name, stud->roll);
}
int main()
{
struct Student stud;
strcpy(stud.name, "techaccess");
stud.roll = 10;
fun (&stud);
return 0;
}
Return the structure from a function:
#include <stdio.h>
#include <string.h>
struct Student
{
char name[10];
int roll;
};
struct Student stud;
struct Student *fun ()
{
printf("\n heee");
strcpy (stud.name, "techaccess");
stud.roll =5;
return &stud;
}
int main()
{
struct Student *stud;
stud = fun ();
printf("\n The student name is: %s and roll number is: %d\n", stud->name, stud->roll);
return 0;
}
Relevant Posts:
- Union in C
- Function in C
- Difference between structure and union
- Structure padding
- Array of structure
- Pointer to structure
Categories: C Language
Leave a Reply