Structures
Introduction
• Arrays have limitations. They are used to store elements of the same type.
• That is, an int array will hold only integers, char array will hold only characters etc.
• We may need to store data items that are logically related but contain dissimilar data
types. E.g employee-id which is integer, employee-name which is character, salary
which may be real etc.
• Structures can be defined as data types that are used to store elements of different
types.
• These dissimilar elements are also stored in contiguous memory locations like arrays.
• Structure can be compared to a record that contains related information. Eg. Record
of a student, record of a patient, record of an employee etc.
Difference between Arrays and structures
Sr Array Structure
1 Used to store elements of same type. Used to store elements of different type.
2 Size of each element is same Size of each member of structure can be different.
3 Declaration : Declaration:
int a[10]; struct student
{ int rno;
char name[20];
} s1,s2,s3;
4 Each element of array is referred with Each member of a structure is referred with the help
the help of an index of a dot operator. Eg. s1.rno.
Defining a structure
• To define a structure, you use the struct keyword. The following illustrates the syntax
of the structure definition:
Syntax..
struct structurename
{ data_type member1;
data_type member2;
data_type member3;
};
Declaring structure variable
• After defining the structure, to work with it we need to create a structure variable.
• There are two ways to declare a structure variable:
• Method-1
-You can declare the structure variable with the structure definition.
Example ….
struct student
{ int rno;
char name[20];
int marks;
} s1,s2,s3;
• Method-2
-You can declare the structure variable after you define the structure.
Example ….
struct student
{ int rno;
char name[20];
int marks;
};
Syntax : struct struct_name var1, var2, var3;
Example : struct student s1,s2,s3;
Accessing members of structures
• Individual elements of an array are accessed using the index. For a structure, individual
members of the structure are accessed using a special operator called as the dot (.)
operator between structure variable and the member.
Syntax : struct_var. member
Eg. : s1.rno = 10;
Initialising structure variables
• The following example demonstrates how to initialize the student structure:
struct student
{ char name[50];
int rno;
} s1 = { "Payal ", 101 };
We defined the student structure, declared and initialized student structure variable.
Nested Structures
• Individual member of a structure can be another structure as well.
• When a structure is declared as the member of another structure, it is called Structure
within a structure.
• It is also known as nested structure.
struct date
{ int day;
int month;
int year;
} d;
struct company
{ char name[20];
int employee_id;
int age;
struct date doj;
} e1;
Accessing Month Field : e1.doj.month
Accessing day Field : e1.doj.day
Accessing year Field : e1.doj.year
Here in this case since date is declared as an individual structure, its members can
also be called independently as follows
d.day;
d.month;
d.year;
Array of Structure
• A variable of type struct student can store details of a single student.
• If we are required to deal with more than one student details, the declaration of so
many variables of struct student type and using those variables would be difficult.
• In such situations we can declare an array of type struct student.
Eg. struct student s[15];
• The above statement defines an array student of size 15.
• Each element of the array s is of type student and can be referred with the index and
can store respective student details.
Example of Structure Array
#include<stdio.h>
struct employee
{ char ename[10];
int sal;
};
void main ()
{ struct employee emp[5];
int i, j;
for(i = 0; i < 3; i++)
{ printf("\nEnter Employee record No : %d ", i+1);
printf("\nEnter Employee Name:");
scanf("%s", emp[i].ename);
printf("\nEnter Employee Sal:");
scanf("%d", &emp[i].sal);
}
printf("\nDisplaying the Employee records");
for(i = 0; i < 3; i++)
{ printf("\nEmployee Name is : %s", emp[i].ename);
printf("\nEmployee Sal is : %d", emp[i].sal);
}
getch();
}
Structure as Function Argument
• We can pass a structure as a function argument just like we pass any other variable or
an array as a function argument.
Example
#include<stdio.h>
struct student
{ char name[10];
int roll;
};
void show(struct student st)
{
printf("\n student name is %s", st.name);
printf("\nroll is %d", st.roll);
}
void main()
{
struct student s;
printf("\nEnter student name : ");
scanf("%s", s.name);
printf("\nEnter student roll no:");
scanf("%d", &s.roll);
printf("\nFunction called to display the details of student ");
show(s);
}
Passing Members of structure as Function Arguments
• We can pass members of structure individually as function arguments.
Example :
#include<stdio.h>
struct student
{ char name[10];
int roll;
};
void show(char nm[ ], int r)
{ printf("\nstudent name is : %s", nm);
printf("\nroll no : %d", r);
}
void main()
{ struct student s;
printf("\nEnter student name : ");
gets(s.name);
printf("\nEnter student rollno : ");
scanf("%d", &s.roll);
printf("\nFunction called to display the details of student ");
show(s.name, s.roll);
getch();
}
Pointers to Structures
• We can define pointers to structures in the same way as we define pointer to any
other variable.
Example:
struct student s;
struct student *ptr;
• Once the pointer is declared, the next step would be to assign the address of a variable
to the pointer.
ptr=&s;
Accessing variables of a Structure with a pointer.
• A special operator -> is used to access the members of structure using pointer.
Syntax : struct_pointer->member;
Example: ptr->rno=10;
Example: Accessing structure elements using pointer
#include <stdio.h>
#include <string.h>
struct stud
{ char name[20];
int rno;
int marks;
};
void main( )
{ struct stud s1, *ptr; // Declare s1 and ptr of type stud
ptr=&s1; // Assign address of s1 to ptr
/*Assigning values to student s1 using pointer */
strcpy( ptr->name, "Rahul");
ptr->rno=100;
ptr->marks=75;
/*Display student details using pointer notation*/
printf( "Student Name : %s\n", ptr->name);
printf( "Student No: %d\n", ptr->rno);
printf( "Student Marks : %d\n", ptr->marks);
getch();
}
Union
• In structure each member has its own storage location, whereas all members
of union use a single shared memory location which is equal to the size of its largest
data member. This single location can accommodate values of different types one at
a time.
• Although a union contains many members of different types, it can handle only one
member at a time. i.e.in this case it cannot work with X and Y at the same time. It can
either hold X or Y.
• A union is declared using the union keyword.
union item
{ int a;
float b;
char c;
} it1;
Accessing a Union Member
Syntax for accessing any union member is similar to accessing structure members,
union test
{ int a;
float b;
char c;
} t;
t.a; /*to access members of union t */
t.b;
t.c;
Difference between Union and Structure
Sr structure union
1 Separate memory is allocated to all All members share common memory.
members.
2 Size depends on all members. Size depends on the largest size
member.
3 All members can maintain data at the Only one member can maintain data at
same time. a time.
4 No overwriting with other members. One member overwrites other
members data.
5 All members can be initialised. All members cannot be initialised.
typedef
• Typedef enables us to rename existing built-in data types and user defined data types
and thereby helps in increasing the readability of source code.
Syntax: typedef old_name new_name;
Where
old_name is the name of the existing datatype.
new_name is the name given to the data type identified by old_name.
Eg. typedef int age;
age a; /*This declares a as variable of type int.*/