Unit 4
Unit 4
Syllabus
Structure
Nested structures
Pointer and Structures
Array of structures
Example Program using structures and pointers
Self referential structures
Dynamic memory allocation
Singly linked list
typedef
Structure Data Type
A structure is a collection of variables under a single name.
A structure is a user defined data type that groups logically
related data items of different data types into a single unit.
All the elements of a structure are stored at contiguous
memory locations.
A variable of structure type can store multiple data items of
different data types under the one name.
As the data of employee in company that is name, Employee
ID, salary, address, phone number is stored in structure data
type.
Need for a structure
Its convenient for grouping logically related
items.
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
Memory Space Allocation
8000
emp_id
8002
name[20]
8022
8024 salary
address[50]
8074
8076
dept_no
8078 employee
age
Declaring a Structure Variable
A structure has to declared, after the body of structure has
defined.
The syntax of declaring a structure is
struct <struct_name> <variable_name>;
The example to declare the variable for defined structure
“employee”
struct employee e1;
Here e1 variable contains 6 members that are defined in
structure.
Initializing a Structure Members
The members of individual structure variable is initialize
one by one or in a single statement.
The example to initialize a structure variable is
1) struct employee e1 = {1, “Hemant”,12000, “3 vikas colony
new delhi”,10, 35);
2) e1.emp_id=1; e1.dept_no=1
e1.name=“Hemant”; e1.age=35;
e1.salary=12000;
e1.address=“3 vikas colony new delhi”;
Accessing a Structure Members
The structure members cannot be directly accessed in
the expression.
They are accessed by using the name of structure
variable followed by a dot and then the name of member
variable.
The method used to access the structure variables are
e1.emp_id, e1.name, e1.salary, e1.address, e1.dept_no,
e1.age.
The data with in the structure is stored and printed by
this method using scanf and printf statement in c
program.
Structure Assignment
The value of one structure variable is assigned to another
variable of same type using assignment statement.
If the e1 and e2 are structure variables of type employee then
the statement
e1 = e2;
assign value of structure variable e2 to e1. The value of each
member of e2 is assigned to corresponding members of e1.
Simple example of a structure
#include<stdio.h>
struct phone
printf("Telephone
{ char *name;
number: %d\n",
int number;
index.number);
};
return 0;
int main()
}
{
struct phone index;
index.name = “Abc";
index.number = 12345;
printf("Name: %s\n",
index.name); Output:
Name: Abc
Telephone number: 12345
Program to implement the Structure
#include <stdio.h>
#include <conio.h>
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
Program to implement the Structure
void main ( )
{ struct employee e1,e2;
printf (“Enter the employee id of employee”);
scanf(“%d”,&e1.emp_id);
printf (“Enter the name of employee”);
scanf(“%s”,e1.name);
printf (“Enter the salary of employee”);
scanf(“%f”,&e1.salary);
printf (“Enter the address of employee”);
scanf(“%s”,e1.address);
printf (“Enter the department of employee”);
scanf(“%d”,&e1.dept_no);
printf (“Enter the age of employee”);
Program to implement the Structure
scanf(“%d”,&e1.age);
printf (“Enter the employee id of employee”);
scanf(“%d”,&e2.emp_id);
printf (“Enter the name of employee”);
scanf(“%s”,e2.name);
printf (“Enter the salary of employee”);
scanf(“%f”,&e2.salary);
printf (“Enter the address of employee”);
scanf(“%s”,e2.address);
printf (“Enter the department of employee”);
scanf(“%d”,&e2.dept_no);
printf (“Enter the age of employee”);
scanf(“%d”,&e2.age);
Program to implement the Structure
printf (“The employee id of employee is : %d”,
e1.emp_id);
printf (“The name of employee is : %s”,
e1.name);
printf (“The salary of employee is : %f”,
e1.salary);
printf (“The address of employee is : %s”,
e1.address);
printf (“The department of employee is : %d”,
e1.dept_no);
printf (“The age of employee is : %d”,
e1.age);
Program to implement the Structure
printf (“The employee id of employee is : %d”,
e2.emp_id);
printf (“The name of employee is : %s”,
e2.name);
printf (“The salary of employee is : %f”,
e2.salary);
printf (“The address of employee is : %s”,
e2.address);
printf (“The department of employee is : %d”,
e2.dept_no);
printf (“The age of employee is : %d”,e2.age);
getch();
}
Output of Program
Enter the employee id of employee 1
Enter the name of employee Rahul
Enter the salary of employee 15000
Enter the address of employee 4,villa area, Delhi
Enter the department of employee 3
Enter the age of employee 35
Enter the employee id of employee 2
Enter the name of employee Rajeev
Enter the salary of employee 14500
Enter the address of employee flat 56H, Mumbai
Enter the department of employee 5
Enter the age of employee 30
Output of Program
The employee id of employee is : 1
The name of employee is : Rahul
The salary of employee is : 15000
The address of employee is : 4, villa area, Delhi
The department of employee is : 3
The age of employee is : 35
The employee id of employee is : 2
The name of employee is : Rajeev
The salary of employee is : 14500
The address of employee is : flat 56H, Mumbai
The department of employee is : 5
The age of employee is : 30
Simple example of a structure
#include<stdio.h>
struct phone
printf("Telephone
{ char *name;
number: %d\n",
int number;
index.number);
};
return 0;
int main()
}
{
struct phone index;
index.name = “Abc";
index.number = 12345;
printf("Name: %s\n",
index.name); Output:
Name: Abc
Telephone number: 12345
C Program to Store Information(name, roll and marks) of
a Student Using Structure
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
struct student s;
printf("Enter information of students:\n\n");
printf("Enter name: ");
scanf("%s",s.name);
printf("Enter roll number: ");
C Program to Store Information(name, roll and
marks) of a Student Using Structure
scanf("%d",&s.roll);
printf("Enter marks: ");
scanf("%f",&s.marks);
printf("\nDisplaying Information\n");
printf("Name: %s\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);
return 0;
}
Sample Output
Enter Information of students:
Enter name: Ankit
Enter roll number: 4600024
Enter marks: 445
Displaying Information
Name: Ankit
Roll : 4600024
Marks: 445
Union Data Type
union employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
Defining a Union
To define a union, we must use the union statement in
very similar was as we did while defining structure.
This means that a single variable i.e.. same memory location can
be used to store multiple types of data.
Memory Space Allocation
8000
emp_id, dept_no, age
8002
salary
8004
name
8022
address
8050
employee
Accessing Union Members
To access any member of a union, we use the member
access operator (.).
Following is the example to explain usage of union:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
}
Output:
data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : Programming
Simple example of Union
#include<stdio.h> int main()
{
typedef union
Simple_union numbers;
myunion
numbers.pi = 3.14;
{ numbers.b = 50;
float pi; return 0;
int b; }
}simple_union;
Major Advantages
• The union data type was invented to
prevent memory fragmentation.
• The union data type prevents
fragmentation by creating a standard
size for certain data.
• Just like with structures, the members of
unions can be accessed with the . and ->
operators.
Difference between Structures & Union
void main( )
{
struct student st;
printf(“\n Enter Student Details \n”);
scanf(“ %s %s %s %d %d %d”, &st.s.regno, &st.s.
name, &st.s.place, &st.mat, &st.phy,&st.chem);
st.total=st.mat + st.phy + st.chem;
st.avg= st.total/3;
printf(“\n The Details of the Student is \n”);
printf(“\n Reg No \t Name \t Place \t Maths \t Physics \t
Chemistry \t Total \t Average\n”);
printf(“%s \t %s \t %s \t %d \t %d \t %d \t %d \t %f”,
st.s.regno, st.s.name, st.s.place, st.mat,st.phy, st.chem,
st.total, st.avg);
getch();
}
Output:
Enter Student Details
40105205065 Ram mmr 87 81 84