Академический Документы
Профессиональный Документы
Культура Документы
ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UNIT – IV
STRUCTURES
Definition of Structures : A Structure is a derived data type. A structure contains one or more data items
of same or different data types in which the individual elements can differ in type. A simple structure may
contain the integer elements, float elements, character elements etc. The individual structure elements are called
members.
struct tag_name
{
type1 member1;
type2 member2;
: :
: :
typeN memberN;
};
Where,
1 : struct : struct is the keyword which tells the compiler that a structure is being defined.
2 : tag_name : is the name of the structure.
3 : member1,member2, ….. , memberN are called members of the structure. They are also called fields of the
structure.
4 : The members are declared within curly braces. The members can be any of the data types such as int, char,
flat, etc.
5 : There should be semicolon at the end of closing brace.
Ex : struct student
{
char name[10];
int roll_number;
float average_marks;
};
The structure definition does not reserve any space in memory for the members. So, it is called a
structure template and memory will not be allocated for the template.
1
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2) How to decleration of the structure?
Declaration of Structures : After defining a structure format we can declare variables of that type. A
structure variable declaration is similar to the declaration of variables of any other data types.
struct student
{
char name[10];
int roll_number; //structure definition.
float average_marks;
};
struct student it1,it2; //structure declaration.
Is same as…
struct student
{
char name[10];
int roll_number;
float average_marks;
} it1,it2;
OR even……
struct
{
char name[10];
int roll_number;
float average_marks;
} it1,it2;
Once the structure definition is associated with variables such as it1 and it2, the compiler allocates
memory for the structure variables. The number of bytes allocated for the variable it1 is
2
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
So, totally 16 bytes are reserved for the variable it1 and another 16 bytes are reserved for the variable it2.
Initilization of Structures : The initializing structure variable is similar to that of arrays i.e, all the
elements will be enclosed within curly braces i.e, ‘{‘ and ‘}’ and are separated by commas.
Where,
Examples
struct student
{
char name[10];
int roll_number;
float average_marks;
struct student
{
char name[10];
int roll_number;
float average_marks;
};
3 : Initilization along with structure declaration with more than one variable.
3
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct student
{
char name[10];
int roll_number;
float average_marks;
struct student
{
char name[10];
int roll_number;
float average_marks;
struct student
{
char name[10];
int roll_number;
float average_marks;
};
struct student it1={“jbrec”, 1000, 75};
struct student it2={“jbrec”, 1000, 75};
NOTE : The members of the structure cannot be initilzed in the structure definition.
Accessing Structures : After declaring the structure type, variables and members, the members of the
structure can be accessed by using the structure variable along with dot(.) operator.
Syntax : variable.member;
Ex : struct student
{
char name[10];
int roll_number;
float average_marks;
};
4
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct student it1;
For accessing the structure members from the
Write a program to print the student number, name and marks through accessing structure elements.
#include<stdio.h>
#include<conio.h>
struct std
{
int no;
char name[10];
int marks;
};
struct std s;
void main()
{
float total,avg;
printf("Enter student details:\n");
printf("\nEnter the student no:");
scanf("%d",&s.no);
printf("\nEnter the student name:");
scanf("%s",&s.name);
printf("\nEnter the student marks:");
scanf("%d",&s.marks);
printf("Rollno\tName\tMarksn");
printf("%d\t%s\t%d”,s.no,s.name,s.marks);
getch();
5
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Ex : if you want to handle more records within one structure, we need not specify the number of structure
variable. Simply we can use array of structure variable to store them in one structure variable.
#include<stdio.h>
#include<conio.h>
struct std
{
int no,marks;
char name[10];
};
struct std s[5];
void main()
{
int i,n;
clrscr();
printf("Enter the number of Records");
scanf("%d",&n);
printf("Enter student details:\n");
for(i=0;i<n;i++)
{
printf("\nEnter the student no:");
scanf("%d",&s[i].no);
printf("\nEnter the student name:");
scanf("%s",&s[i].name);
printf("\nEnter the student marks:");
scanf("%d",&s[i].marks);
}
printf("Rollno\tName\tMarks\n");
for(i=0;i<n;i++)
printf("%d\t%s\t%d\n",s[i].no,s[i].name,s[i].marks);
getch();
Nested Structure : if a structure contains more than one structure as its members is known as a nested
structure. i.e, structure within another structure. It is used to increase the readability of the program by reducing
the complexity.
Ex :Write a program to read and write employee and their date of joining using nested structure.
#include<stdio.h>
#include<conio.h>
struct date
6
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{
int day;
char month[20];
int year;
};
struct employee
{
int code;
char name[30];
float salary;
struct date doj;
};
struct employee emp1;
void main()
{
printf("Enter Employee code:");
scanf("%d",&emp1.code);
printf("Enter Employee name:");
scanf("%s",&emp1.name);
printf("Enter Employee salary:");
scanf("%f",&emp1.salary);
printf("Enter Employee date of joining:");
scanf("%d%s%d",&emp1.doj.day,&emp1.doj.month,&emp1.doj.year);
printf("Display the Employee Details\n");
printf("Code\tName\tSalary\tDOJ\n");
printf("%d\t%s\t%.2f\t%d%s%d",emp1.code,emp1.name,emp1.salary,emp1.doj.day,emp1.doj.month,emp1.doj.
year);
getch();
}
Structures and functions: The general format of sending a copy of a structure to the called function is:
Function_name(structure_variable_name);
struct emp
{
char name[30];
int points;
double sal;
};
struct emp update(struct emp e, int p, double s);
void main()
{
int p_inc;
double sal_inc;
struct emp e={“urname”, 100, 30000};
printf(“enter points increment and salary increment”);
scanf(“%d%f”,&p_inc,&sal_inc);
e=update(struct emp e, p_inc, sal_inc);
printf(“updated values of employee”);
printf(“name :%s\n”, e.name);
printf(“Points :%d\n”, e.points);
printf(“Salary :%lf\n”, e.sal);
getch();
}
struct emp update(struct emp e1, int p, double s)
{
e1.points+=p;
e1.sal+=s;
return(e1);
}
Self referential structures : A structure consists of atleast a pointer member pointing to the same structure
is known as a self-referential structure.
8
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Where *name refers to the name of a pointer variable.
Pointers To Structures : we know that a pointer is a variable which contains address of another variable.
A variable which contains address of a structure variable is called pointer to a structure. A pointer to
a structure is created as follows.
Ex :
#include<stdio.h>
#include<conio.h>
struct student
{
char name[10];
int rno;
int marks;
}std1;
void main()
{
struct student *pt;
clrscr();
printf("\nEnter the student details:");
printf("\nEnter the student name:");
scanf("%s",std1.name);
printf("\nEnter the rno:");
scanf("%d",&std1.rno);
printf("\nEnter the marks:");
scanf("%d",&std1.marks);
pt=&std1;
printf("\n Display of structure using structure variable");
printf("\nRollNo\tName\tMarks");
printf("\n%s\t%d\t%d",std1.name,std1.rno,std1.marks);
printf("\nDisplay of structure using pointer variable");
printf("\nRollNo\tName\tMarks");
printf("\n%s\t%d\t%d",pt->name,pt->rno,pt->marks);
getch();
9
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
Typedef Declaration : The typedef is a keyword that allows the programmer to create a new data type
name for an existing data type. So, the purpose of typedef is to redefine the name of an existing variable type.
Where
1 : typedef : is a keyword.
2 : data type : any Existing Data Type.
3 : New Names for the existing Data Type.
Advantages of typedef :
#include<stdio.h>
#include<conio.h>
void main()
{
typedef int digits;
digits a,b,sum;
clrscr();
printf("Enter a and b values:");
scanf("%d%d",&a,&b);
sum=a+b;
printf("The sum is:%d",sum);
getch();
}
10
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
For Example : we can take integers 1, 2, 3, ……12 to represent months for easy programming. It is more
readable and understandable if we replace these numbers by some meaningful and descriptive names such as
jan, feb, mar,…Dec. This concept of replacing integers by some meaningful and descriptive names gives rise to
new data type called enumerated data type.
Declaration of variable
Example 1 :
#include<stdio.h>
#include<conio.h> OUTPUT : 2
void main()
{
enum months
{
jan,feb,mar,apr,may,jun,july,aug,sep,oct,nov,dec
};
clrscr();
printf("%d",mar);
getch();
}
Example 2 :
#include<stdio.h>
#include<conio.h> OUTPUT :
void main() Monday is : 1
{
enum days
{
sun,mon,tue,wed,thu,fri,sat
}d1,d2;
clrscr();
d1=sun;
printf("Monday is:%d",d1+1);
getch();
11
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
BitFields : The bit field is a special feature available in C language. Which is normally is used to manipulate
the bits. A group of several bits can be packed together using a structure. Since bit fields are defined within a
structure or union, the various bits can be accessed in a way we acess individual members of a structure or unin.
12
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
clrscr();
printf("Enter the number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the information of student =%d\n",i+1);
printf("Name :");
scanf("%s",s[i].name);
printf("Age :");
scanf("%d",&age);
printf("RollNo :");
scanf("%d",&rollno);
printf("Branch :");
scanf("%s",s[i].branch);
s[i].age=age;
s[i].rollno=rollno;
}
printf("Name\t Age\t RollNo\t Branch\n");
for(i=0;i<n;i++)
{
printf("%s\t\%d\t%d\t%s\n",s[i].name,s[i].age,s[i].rollno,s[i].branch);
}
getch();
}
Union : Union is a derived type and it is declared like structure. The difference between union and structure is
in terms of storage. In structure each member has its own storage location, whereas all the members of union
use the same location, although a union may contain many members of different types. When we use union the
compiler allocates a piece of storage that is larger enough to hold. Like structures, union is also declared by
using the keyword union.
13
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 : union_name : is the name of union.
3 : member1,member2, ….. , memberN are called members of the structure. They are also called fields of the
structure.
4 : The members are declared within curly braces. The members can be any of the data types such as int, char,
flat, etc.
5 : There should be semicolon at the end of closing brace.
Ex : union student
{
int sno,smarks;
char sname[10];
};
Structure Union
1 : The keword struct is used to define a 1 : The keyword union is used to define a
structure.y union.
2 : When a variable is associated with a 2 : When a variable is associated with a
structure, the compiler allocates the memory union, the compiler allocates the memory by
for each member. The size of structure is considering the size of the largest member.
greater than or equal to the sum of sizes of its So size of union is equal to the size of largest
members. The smaller members may end number.
with unused slack bytes.
3 : Each member within a structure is 3 : Memory allocated is shared by individual
assigned unique storage area. members of union.
4 : The address of each member will be in 4 : The address is same for all the members of
asecending order. This indicates that memory a union. This indicates that every member
for each member will start at different offset begins at offset values.
values.
5 : Alterning the value of a member will not 5 : Alterning the value of any of the member
affect other members of the structure. will alter other member values.
6 : Individual members can be accessed at a 6 : Only one member can be accessed at a
time. time.
7 : Several members of a structure can be 7 : Only the first member of a unin can be
initialized at once. initialized.
14
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Definition of Structures : A Structure is a derived data type. A structure contains one or more data items
of same or different data types in which the individual elements can differ in type. A simple structure may
contain the integer elements, float elements, character elements etc. The individual structure elements are called
members.
struct tag_name
{
type1 member1;
type2 member2;
: :
: :
typeN memberN;
};
Where,
1 : struct : struct is the keyword which tells the compiler that a structure is being defined.
2 : tag_name : is the name of the structure.
3 : member1,member2, ….. , memberN are called members of the structure. They are also called fields of the
structure.
4 : The members are declared within curly braces. The members can be any of the data types such as int, char,
flat, etc.
5 : There should be semicolon at the end of closing brace.
Ex : struct student
{
char name[10];
int roll_number;
float average_marks;
};
The structure definition does not reserve any space in memory for the members. So, it is called a
structure template and memory will not be allocated for the template.
Declaration of Structures : After defining a structure format we can declare variables of that type. A
structure variable declaration is similar to the declaration of variables of any other data types.
15
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct student
{
char name[10];
int roll_number; //structure definition.
float average_marks;
};
struct student it1,it2; //structure declaration.
Is same as…
struct student
{
char name[10];
int roll_number;
float average_marks;
} it1,it2;
OR even……
struct
{
char name[10];
int roll_number;
float average_marks;
} it1,it2;
Once the structure definition is associated with variables such as it1 and it2, the compiler allocates
memory for the structure variables. The number of bytes allocated for the variable it1 is
So, totally 16 bytes are reserved for the variable it1 and another 16 bytes are reserved for the variable it2.
Initilization of Structures : The initializing structure variable is similar to that of arrays i.e, all the
elements will be enclosed within curly braces i.e, ‘{‘ and ‘}’ and are separated by commas.
16
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Where,
Examples
struct student
{
char name[10];
int roll_number;
float average_marks;
struct student
{
char name[10];
int roll_number;
float average_marks;
};
3 : Initilization along with structure declaration with more than one variable.
struct student
{
char name[10];
int roll_number;
float average_marks;
struct student
{
char name[10];
int roll_number;
17
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
float average_marks;
struct student
{
char name[10];
int roll_number;
float average_marks;
};
struct student it1={“jbrec”, 1000, 75};
struct student it2={“jbrec”, 1000, 75};
NOTE : The members of the structure cannot be initilzed in the structure definition.
Accessing Structures : After declaring the structure type, variables and members, the members of the
structure can be accessed by using the structure variable along with dot(.) operator.
Syntax : variable.member;
Ex : struct student
{
char name[10];
int roll_number;
float average_marks;
};
struct student it1;
For accessing the structure members from the
Write a program to print the student number, name and marks through accessing structure elements.
#include<stdio.h>
#include<conio.h>
struct std
{
int no;
char name[10];
int marks;
};
18
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct std s;
void main()
{
float total,avg;
printf("Enter student details:\n");
printf("\nEnter the student no:");
scanf("%d",&s.no);
printf("\nEnter the student name:");
scanf("%s",&s.name);
printf("\nEnter the student marks:");
scanf("%d",&s.marks);
printf("Rollno\tName\tMarksn");
printf("%d\t%s\t%d”,s.no,s.name,s.marks);
getch();
19