0% found this document useful (0 votes)
118 views48 pages

Unit 4

The document discusses structures and unions in C programming. It defines a structure as a user-defined data type that groups related data items under a single name. A union is similar, but allocates space for the largest member and allows different types to share the same memory space. The document provides examples of defining, declaring, initializing, and accessing structures and unions, and includes a full program demonstrating the use of structures.

Uploaded by

arulmani1979
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views48 pages

Unit 4

The document discusses structures and unions in C programming. It defines a structure as a user-defined data type that groups related data items under a single name. A union is similar, but allocates space for the largest member and allows different types to share the same memory space. The document provides examples of defining, declaring, initializing, and accessing structures and unions, and includes a full program demonstrating the use of structures.

Uploaded by

arulmani1979
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Unit V

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.

 Structures are generally useful whenever a lot of


data needs to be grouped together--for instance,
they can be used to hold records from a database
or to store information about contacts in an
address book.

 In the contacts example, a struct could be used


that would hold all of the information about a
single contact--name, address, phone number, and
so forth.
Defining of Structure
A structure has to be defined, before it can be used. The
syntax of defining a structure is
struct <struct_name>
{
<data_type> <variable_name>;
<data_type> <variable_name>;
……..
<data_type> <variable_name>;
};
Example of Structure

The structure of Employee is declared as

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

A union is a user defined data type like structure.


The union groups logically related variables into a
single unit.
The union data type allocate the space equal to space
need to hold the largest data member of union.
The union allows different types of variable to share
same space in memory.
There is no other difference between structure and union
than internal difference.
The method to declare, use and access the union is same
as structure.
Defining of Union
A union has to defined, before it can used. The syntax of
defining a structure is
union <union_name>
{
<data_type> <variable_name>;
<data_type> <variable_name>;
……..
<data_type> <variable_name>;
};
Example of Union

The union of Employee is declared as

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.

 The format of the union statement is as follows:


union [union tag]
{
member definition;
member definition; ...
member definition;
} [one or more union variables];
Defining a Union (cont..)
 The union tag is optional and each member definition is a
normal variable definition, such as int i; or float f; or any other
valid variable definition.

 At the end of the union's definition, before the final semicolon,


you can specify one or more union variables but it is
optional.
 Here is the way you would define a union type named ABC which
has the three members i, f, and str:
Defining a Union (cont..)
union ABC
{
int i;
float f;
char str[20];
} data;

 Now a variable of ABC type can store an integer, a floating-point


number, or a string of characters.

 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

1) The memory occupied by structure variable is


the sum of sizes of all the members but
memory occupied by union variable is equal
to space hold by the largest data member
of a union.
2) In the structure all the members are
accessed at any point of time but in union
only one of union member can be accessed
at any given time.
Application of Structures

•Structure is used in database management to


maintain
1. data about books in library,
2. items in store,
3. employees in an organization,
4. financial accounting transaction in
company.
Beside that other application are
1) Changing the size of cursor.
2) Clearing the contents of screen.
3) Drawing any graphics shape on screen.
4) Receiving the key from the keyboard.
Union vs. Struct
• Similarities
– Definition syntax virtually identical
– Member access syntax identical
• Differences
– Members of a struct each have their own address in
memory.
– The size of a struct is at least as big as the sum of the
sizes of the members (more on this later)
– Members of a union share the same memory.
– The size of a union is the size of the largest member.
Structure program
#include <stdio.h>
#include <conio.h>
struct employee
{
int empid;
char ename[15];
int basic;
float hra;
float da;
float it;
float gross;
float netpay;
}emp[50];
void main()
{
int i, j, n;
clrscr();
printf("\nEnter No. of Employees : ");
scanf("%d", &n);
for(i=0; i<n ;i++)
{
printf("\nEnter Employee Details\n");
printf("Enter Employee Id : ");
scanf("%d", &emp[i].empid);
printf("Enter Employee Name : ");
scanf("%s", emp[i].ename);
printf("Enter Basic Salary : ");
scanf("%d", &emp[i].basic);
}
for(i=0; i<n; i++)
{
emp[i].hra = 0.02 * emp[i].basic;
emp[i].da = 0.01 * emp[i].basic;
emp[i].it = 0.05 * emp[i].basic;
emp[i].gross = emp[i].basic+emp[i].hra+emp[i].da;
emp[i].netpay = emp[i].gross - emp[i].it;
}
printf("\n\n\n\t\t\t\t XYZ & Co. Payroll \n\n");
for(i=0;i<80;i++)
printf("*");
printf("\nEmpId\tName\t\tBasic\t HRA\t DA\t IT\tGross\t\t
Net Pay \n\n");
for(i=0;i<80;i++)
printf("*");
for(i=0; i<n; i++)
{
printf("\n%d\t%-5s\t%d\t%.2f\t%.2f\t%.2f \t %.2f \t
%.2f", emp[i].empid,emp[i].ename, emp[i].basic,
emp[i].hra, emp[i].da, emp[i].it,emp[i].gross,
emp[i].netpay);
}
printf("\n");
for(i=0;i<80;i++)
printf("*");
getch();
}
Output
Enter No. of Employees : 2
Enter Employee Details
Enter Employee Id : 436

Enter Employee Name : Gopal


Enter Basic Salary : 10000
Enter Employee Details
Enter Employee Id : 463

Enter Employee Name : Rajesh


Enter Basic Salary : 22000
XYZ & Co. Payroll
*************************************************************************
EmpId Name Basic HRA DA IT Gross Net Pay
*************************************************************************
436 Gopal 10000 200.00 100.00 500.00 10300.00 9800.00
463 Rajesh 22000 440.00 220.00 1100.00 22660.00 21560.00
************************************************************************
NESTED STRUCTURE (use two
structure variables)
#include<stdio.h>
#include<conio.h>
struct stud
{
char regno[20];
char name[25];
char place[20];
};
struct student
{
int mat, phy, chem, total;
float avg;
struct stud s;
};

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

The Details of the Student is

Regno Name Place Maths Physics Chemistry Total Average


65 Ram mmr 87 81 84 252 84.00

You might also like