Unit-4 Structure
Unit-4 Structure
very helpful in cases where we need to store similar data of multiple entities
Structures (also called structs) are a way to group several related variables into one
place. Each variable in the structure is known as a member of the structure.
Unlike an array, a structure can contain many different data types (int, float, char,
etc.). In C, there are cases where we need to store multiple attributes of an entity. It
is not necessary that an entity has all the information of one type only. It can have
different attributes of different data types. For example, an entity Student may
have its name (string), roll number (int), marks (float). To store such type of
information regarding an entity student, we have the following approaches:
o Construct individual arrays for storing names, roll numbers, and marks.
o Use a special data structure to store the collection of different data types.
#include<stdio.h>
void main ()
{
char names[2][10],dummy; // 2-
dimensioanal character array names is used to store the names of the students
int roll_numbers[2],i;
float marks[2];
for (i=0;i<3;i++)
{
printf("Enter the name, roll number, and marks of the student %d",i+1);
scanf("%s %d %f",&names[i],&roll_numbers[i],&marks[i]);
scanf("%c",&dummy); // enter will be stored into dummy character at each iterat
ion
}
printf("Printing the Student details ...\n");
for (i=0;i<3;i++)
{
printf("%s %d %f\n",names[i],roll_numbers[i],marks[i]);
}
}
Output
Enter the name, roll number, and marks of the student 1Arun 90 91
Enter the name, roll number, and marks of the student 2Varun 91 56
Enter the name, roll number, and marks of the student 3Sham 89 69
The above program may fulfill our requirement of storing the information of an
entity student. However, the program is very complex, and the complexity increase
with the amount of the input. The elements of each of the array are stored
contiguously, but all the arrays may not be stored contiguously in the memory. C
provides you with an additional and simpler approach where you can use a special
data structure, i.e., structure, in which, you can group all the information of
different data type regarding an entity.
Q. What is structure in C?
The structure in C is a user-defined data type that can be used to group
items of possibly different types into a single type.
Structure-function can be used to write code effectively
A structure is a custom data type that holds multiple members of different
data type under a single unit
Unlike an array, a structure can contain many different data types (int, float,
char, etc.).
Structure in c is a user-defined data type that enables us to store the collection of
different data types. Each element of a structure is called a member. Structures ca;
simulate the use of classes and templates as it can store various information
The ,struct keyword is used to define the structure. Let's see the syntax to define
the structure in c.
1. struct structure_name
2. {
3. data_type member1;
4. data_type member2;
5. .
6. .
7. data_type memeberN;
8. };
1. struct employee
2. { int id;
3. char name[20];
4. float salary;
5. };
The following image shows the memory allocation of the structure employee that
is defined in the above example.
Here, struct is the keyword; employee is the name of the structure; id, name,
and salary are the members or fields of the structure. Let's understand it by the
diagram given below:
Advantages Of Structures:
Now structure variables can be created using struct student s1, s2; and these
variables can store all the details of two students.
struct student
{
int roll_number;
char name[30];
float marks;
char address[50];
};
struct complex
{
float real;
float imaginary;
};
Defining a Structure:
Define structure in C
Structure variables must first be defined before being created. Structures may be
defined using the struct keyword.
Syntax:-
struct name_of_the_structure
data_type member1;
data_type member2;
...
data_type memberN;
};
Example of C Structures
struct bill
float amount;
int id;
char address[100];
};
To make it simple to access a structure member, we can create a variable for the
structure. Structure variables can be declared in one of two ways:
struct employee
{ int id;
char name[50];
float salary;
};
Put the provided code within the main() method at this point.
The values kept in the structure can be accessed using the variables e1 and e2.
Here, e1 and e2 may be handled similarly to Java and C++ objects.
2nd way:
Let’s see a different approach to declaring variables as the structure is being
defined.
struct employee
{ int id;
char name[50];
float salary;
}e1,e2;
Let’s look at the code for the (member) operator to access the id member of the p1
variable.
p1.id
Example:
Example:
Note: The values for the value initialized structure should match the order in
which structure members are declared.
Invalid initialization:
The above code will throw compilation error. Since the order of member type in
structure is character array, integer finally float. But, we aren’t initializing the
structure variable in the same order.
The above approach may suit all needs. In addition, C language supports flexibility
to initialize structure members in any order. I know this sounds bit confusing. As,
just now I said C will throw error if you try to initialize members in different order
of declaration.
This approach is an extension of above. Here, you can specify member name along
with the value.
Example:
// Declare and initialize structure variable
struct student stu1 = {
.roll = 12,
.name = "Pankaj",
.marks = 79.5f
};
Copy
Initializing all fields to NULL is bit cumbersome process. Let’s do a small hack to
initialize structure members to default value, on every structure variable
declaration.
Example:
/**
* How to declare, initialize and access structures in C language
*/
#include <stdio.h>
int main()
{
// Declare structure variable with default initialization
struct student stu1 = NEW_STUDENT;
return 0;
}
O/P-
Student details
Roll : 12
Marks: 79.50