structures
Dr [Link] Mohan
Professor, CSE
• a structure is a composite data type (derived from primitive data
types such as int and float) that we use in order to define a
collection of similar or different data types under one same
name in a particular block of computer memory.
Declaring a structure
Example 1: Example 2:
Syntax: struct point2d struct point3d
{ {
struct <structure name> int x; float x;
{ int y; float y;
structure element 1; }; float z;
structure element 2; };
structure element 3;
Example 3:
…
}; struct book
{
char name;
float price;
int pages;
};
Write a C program to calculate the distance between
the two points.
Write a C program to calculate the distance between
the two points.
#include <stdio.h>
#include <math.h>
int main()
{
float x1, y1, x2, y2, gdistance;
printf("Input x1: ");
scanf("%f", &x1);
printf("Input y1: ");
scanf("%f", &y1);
printf("Input x2: ");
scanf("%f", &x2);
printf("Input y2: ");
scanf("%f", &y2);
gdistance = ((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));
printf("Distance between the said points: %.4f", sqrt(gdistance));
printf("\n");
return 0;
}
Write a C program to calculate the distance between
the two points. using … structures
#include <stdio.h>
#include <math.h>
struct point
{
int x;
int y;
};
int main()
{
struct point a, b;
float distance;
printf("Enter coordinate of point a: ");
scanf("%d %d", &a.x, &a.y);
printf("Enter coordinate of point b: ");
scanf("%d %d", &b.x, &b.y);
distance = sqrt(pow((a.x - b.x),2) + pow((a.y-b.y),2));
printf("Distance between a and b: %lf\n", distance);
return 0;
}
❖ Library Management System
• Library management system is a project which aims in developing a
computerized system to maintain all the daily work of library. This project
has many features, like facility of users (i.e student, staff ) login. It also
has a facility of admin login through which the admin can monitor the
whole system. It has a facility where student after logging in their accounts
can see list of books issued and its issue date and return date and also
the students can request the librarian to add new books by filling the book
request form. The librarian after logging into his account i.e admin account
can generate various reports such as student report, issue report,
teacher report and book report. Overall this project, reduces the human
efforts.
• person • student
number, person,
name, class,
date, section,
sex, fees,
• date taddress,
dd, paddress.
mm, • staff
yy, person,
qualification,
designation,
department,
• address salary,
• book
door number, name,
street, author,
village/town, price,
district. pages,
date,
address.
struct date
• date {
dd, int dd;
mm, int mm;
yy, int yy;
};
struct address
• address {
door number, char door_number[12];
street, char street[20];
village/town, char village[15],
district. char district[15];
};
struct person
{
int number;
char name[20]; struct date
struct date {
{ int dd;
int dd;
int mm; int mm;
int yy; int yy;
• person } dob; };
number, char gender;
struct address
name,
{
date, char door_number[12];
sex, char street[20];
taddress, char village[15], struct address
paddress. char district[15]; {
} taddress; char door_number[12];
struct address char street[20];
{
char door_number[12]; char village[15],
char street[20]; char district[15];
char village[15], };
char district[15];
} paddress;
}
struct book
{
char name[20]; struct date
char author[20]; {
float price, int dd;
int pages, int mm;
• book struct date int yy;
name, { };
author, int dd;
price, int mm;
pages, int yy;
date,
} pubdate; struct address
address.
struct address {
{ char door_number[12];
char door_number[12]; char street[20];
char street[20]; char village[15],
char village[15], char district[15];
char district[15]; };
} pubaddess;
};
struct student
{ struct person
struct person {
{
int number;
int number;
char name[20]; char name[20];
struct date struct date
{ {
int dd; int dd;
int mm; int mm;
int yy; int yy;
} dob;
} dob;
• student char gender;
struct address char gender;
person, { struct address
char door_number[12]; {
class, char street[20]; char door_number[12];
section, char village[15], char street[20];
char district[15];
fees, char village[15],
} taddress;
struct address char district[15];
{ } taddress;
char door_number[12]; struct address
char street[20]; {
char village[15], char door_number[12];
char district[15];
char street[20];
} paddress;
} p; char village[15],
char class[10]; char district[15];
char section[10]; } paddress;
float fees; }
}
struct staff
{
struct person struct person
{ {
int number; int number;
char name[20]; char name[20];
struct date struct date
{ {
int dd;
int mm;
int dd;
int yy; int mm;
} dob; int yy;
• staff char gender;
struct address
} dob;
char gender;
person, { struct address
char door_number[12];
qualification, char street[20];
{
designation, char village[15], char door_number[12];
char district[15]; char street[20];
department, } taddress; char village[15],
salary, struct address char district[15];
{ } taddress;
char door_number[12];
struct address
char street[20];
char village[15], {
char district[15]; char door_number[12];
} paddress; char street[20];
} p; char village[15],
char qualification[20]; char district[15];
char designation[20]; } paddress;
char department[10];
float salary;
}
}
struct person struct student
{ {
struct date int number; struct person p;
char name[20]; char class[10];
{ struct date dob; char section[10];
int dd;
char gender; float fees;
int mm;
struct address taddress; }
int yy;
struct address paddress;
} }
struct staff
{
struct address struct book struct person p;
char qualification[20];
{ { char designation[20];
char door_number[12]; char name[20]; char department[10];
char street[20]; char author[20]; float salary;
char village[15], float price,
char district[15]; int pages, }
} struct date pubdate;
struct address pubaddess;
}
Declaring structure variables
Example:
struct book struct book
{ {
char name; char name;
float price; float price;
int pages; int pages;
}b1, b2, b3; };
struct book b1, b2, b3;
struct - - - -
{
char name;
float price;
int pages;
}b1, b2, b3;
Initialising structure variables
Example:
struct book
{
char name[10];
float price;
int pages;
};
struct book b1 = {“basic”, 130.00,500};
struct book b2 = {“Cobol”, 130.00,500};
Krishna Mohan. G
structure variables Assignment
Example:
struct book
{
char name[10];
float price;
int pages;
};
struct book b1 = {“basic”, 130.00,500};
struct book b2;
b2=b1;
Krishna Mohan. G
#include <stdio.h>
struct point
Assignment of a structure variable to another structure
{ variable.
int x;
int y;
};
int main()
{
struct point p1,p2;
p1.x=10;
p1.y=11;
p2=p1;
printf("Address of p1 = %p and the point is (%d,%d)\n", &p1,p1.x,p1.y);
printf("Address of p2 = %p and the point is (%d,%d)\n", &p2,p2.x,p2.y);
p2.x=20;
p2.y=21;
printf("Address of p1 = %p and the point is (%d,%d)\n", &p1,p1.x,p1.y);
printf("Address of p2 = %p and the point is (%d,%d)\n", &p2,p2.x,p2.y);
return 0;
}
Accessing structure elements
•2 ways
1. using ‘.’ operator (dot)
2. using ‘->’ operator (arrow)
Example: Struct book
{
char name[10];
float price;
int pages;
};
Struct book b1 = {“basic”, 130.00,500};
Struct book *b2;
b2 = &b1;
Using ‘dot’ operator Using ‘arrow’ operator
[Link]; b2->name;
[Link]; b2->price;
[Link]; Krishna Mohan. G b2->pages;
How structure elements are stored
Example: Struct book
{
char name[10];
float price;
int pages;
};
Struct book b1 = {“basic”, 130.00,500};
Struct book *b2;
b2=&b1;
1000 [Link] [Link] [Link];
basic 130.00 500
10-bytes 4-bytes 2-bytes
b2 1000
2-bytes
To calculate the distance between the two points.
using … pointer variables of structure
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
struct point struct point
{ {
int x; int x;
int y; int y;
}; };
int main() int main()
{ {
struct point a, b; struct point *a, *b,p,q;
float distance; float distance;
printf("Enter coordinate of point a: "); p.x=2; p.y=2; q.x=14; q.y=7; a=&p; b=&q;
scanf("%d %d", &a.x, &a.y); printf("Enter coordinate of point a: ");
printf("Enter coordinate of point b: "); scanf("%d%d", &a->x, &a->y);
scanf("%d %d", &b.x, &b.y); printf("Enter coordinate of point b: ");
distance = sqrt(pow((a.x - b.x),2) + pow((a.y-b.y),2)); scanf("%d%d", &b->x, &b->y);
printf("Distance between a and b: %lf\n", distance); distance = sqrt(pow((a->x - b->x),2) + pow((a->y-b->y),2));
return 0; printf("Distance between a and b: %lf\n", distance);
} return 0;
}
Array of structures
Example: Struct book
{
char name[10];
float price;
int pages;
};
Struct book b[100];
b[0] b[1] b[2] b[3]
18-bytes 18-bytes 18-bytes …
Krishna Mohan. G
#include <stdio.h>
#include <stdlib.h>
To demonstrate the array of structures.
#include <math.h>
struct point
{
int x;
int y;
};
int main()
{
struct point p[5];
int i;
printf("Enter 3 coordinates of a triangle...\n");
for(i=0;i<3;i++)
{
printf("Enter coordinates of point p[%d]: ",i);
scanf("%d%d", &p[i].x, &p[i].y);
}
printf("size of structure in memory = %d\n",sizeof(struct point));
for(i=0;i<3;i++)
printf("Address of p[%d] = %p\n", i,&p[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h> To calculate the distance between the points of a
#include <math.h>
struct point TRIANGLE.
{
double x;
double y;
};
int main()
{
struct point p[5];
double distance;
int i;
printf("Enter 3 coordinates of a triangle...\n");
for(i=0;i<3;i++)
{
printf("Enter coordinates of point p[%d]: ",i);
scanf("%lf%lf", &p[i].x, &p[i].y);
}
for(i=0;i<3;i++)
{
distance = sqrt(pow((p[i%3].x - p[(i+1)%3].x),2) + pow((p[i%3].y-p[(i+1)%3].y),2));
//printf("(%lf,%lf) and (%lf,%lf)",p[i%3].x,p[i%3].y,p[(i+1)%3].x,p[(i+1)%3].y);
printf("Distance between p[%d] and p[%d]: %lf\n", i%3,(i+1)%3,distance);
}
return 0;
}
Nested structures
The structure can be nested in the following different ways:
1. By separate nested structure
• In this method, the two structures are created, but the dependent structure
should be used inside the main structure as a member.
2. By embedded nested structure.
• Using this method, allows to declare structure inside a structure and it requires
fewer lines of code.
• Error will occur if the structure is present but the structure variable is missing.
#include <stdio.h>
#include <stdlib.h> To print the size of
struct date
{ variables & pointer
};
int dd, mm, yy; variables of structures.
struct person
{
char name[20];
struct date birth_date;
char gender;
};
struct student
{
int no;
char name[20];
struct address
{
char doorno[10];
char village[20];
char district[15];
}addr;
float fee;
};
int main()
#include <stdio.h>
struct date
{
int dd, mm, yyyy;
};
struct person
{
char name[20];
struct date birth_date;
char gender;
};
struct student
{
int no;
char name[20];
struct address
{
char doorno[10];
char village[20];
char district[15];
}addr;
float fee;
};
int main()
{
struct person p1,*p2;
struct student s1,*s2;
strcpy([Link],"gkm");
p1.birth_date.dd=25;
p1.birth_date.mm=8;
p1.birth_date.yyyy=1976;
[Link]='m';
#include <stdio.h>
#include <stdlib.h>
struct date
{
int dd, mm, yy;
};
struct person
{
char name[20];
struct date birth_date;
char gender;
};
struct student
{
int no;
char name[20];
struct address
{
char doorno[10];
char village[20];
char district[15];
}addr;
float fee;
};
int main()
{
struct date d1,*d2;
struct person p1={"NISCHAL",{31,07,2009},'m'},*p2;
struct student s1={1,"krishna mohan",{"20-21- 11","Vijayawada","NTR"},1200},*s2;
p2=&p1; s2=&s1; d2=&d1;
printf("size of DATE ordinary variable = %d\tsizeof DATE pointer variable = %d\n",sizeof(d1),sizeof(d2));
typedef
• The typedef is a keyword that is used in C programming to provide
existing data types with a new name.
• typedef keyword is used to redefine the name already the existing
name.
• When names of datatypes become difficult to use in programs,
typedef is used with user-defined datatypes, which behave similarly
to defining an alias.
• Syntax: typedef <existing_name> <alias_name>
#include <stdio.h> #include <stdio.h>
int main() typedef long long ll;
{ int main()
long long var = 200000; {
printf("%ld", var); ll var = 200000;
return 0; printf("%ld", var);
} return 0;
}
#include <stdio.h>
typedef long long ll;
int main()
{
// long long var = 200000;
ll var=200000;
printf(“value of variable = %ld", var);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef int day;
int main()
{
//int weekday[7]={0,1,2,3,4,5,6};
day weekday[7]={0,1,2,3,4,5,6};
printf("typedef DEMO\n");
for(int i=0;i<7;i++)
printf("day %d\n",weekday[i]);
return 0;
}
// C program to implement
// typedef with structures
#include <stdio.h>
#include <string.h>
typedef struct student
{
char name[25];
char branch[35];
int id;
} students;
// Driver code
int main()
{
students st;
strcpy([Link],"krishna mohan");
strcpy([Link],"Computer Science And Engineering");
[Link] = 3649;
printf("Name: %s\n", [Link]);
printf("Branch: %s\n", [Link]);
printf("ID_no: %d\n", [Link]);
return 0;
}
Additional features of structures…
•A structure variable can be passed to a function.
•Individual structure elements
•Entire structure at one go
Krishna Mohan. G
union
Queries…
Krishna Mohan. G