Structures
Structures
Structures
What is a Structure?
1
03/24/2020
Defining a Structure
struct tag {
member 1;
member 2;
:
member m;
};
Contd.
2
03/24/2020
Example
• A structure definition:
struct student {
char name[30];
int roll_number;
int total_marks;
char dob[10];
};
A new data-type
Programming and Data Structure 5
A Compact Form
struct tag {
member 1;
member 2;
:
member m;
} var_1, var_2,…, var_n;
3
03/24/2020
Equivalent Declarations
struct student {
char name[30];
int roll_number;
int total_marks;
char dob[10];
} a1, a2, a3;
struct {
char name[30];
int roll_number;
int total_marks;
char dob[10];
} a1, a2, a3;
Processing a Structure
4
03/24/2020
#include <stdio.h>
main()
{
struct complex
{
float real;
float cmplex;
} a, b, c;
5
03/24/2020
Arrays of Structures
6
03/24/2020
• General syntax:
typedef struct {
member-variable1;
member-variable2;
.
member-variableN;
} tag;
typedef : An example
typedef struct {
float real;
float imag;
} _COMPLEX;
_COMPLEX a, b, c;
_COMPLEX complexarray[100];
7
03/24/2020
Structure Initialization
a.real=1.0; a.imag=2.0;
b.real=-3.0; b.imag=4.0;
tmp = a;
a = b;
b = tmp;
}
8
03/24/2020
An Example
#include <stdio.h>
typedef struct {
float real;
float imag;
} _COMPLEX;
tmp = a;
a = b;
b = tmp;
}
Example:: contd.
void print (_COMPLEX a)
{
printf("(%f, %f) \n“, a.real, a.imag);
}
main()
{
_COMPLEX x = {4.0,5.0}, y = {10.0,15.0};
print(x); print(y);
swap(x,y);
print(x); print(y);
}
9
03/24/2020
• Output:
(4.000000, 5.000000)
(10.000000, 15.000000)
(4.000000, 5.000000)
(10.000000, 15.000000)
Returning structures
return(tmp);
}
10
03/24/2020
11
03/24/2020
(a) Example 1
#include <stdio.h>
int main()
{
struct A { Here, x (int) is followed by z
// sizeof(int) = 4 (double), which is larger in size
int x; than x. Hence padding is required
// Padding of 4 bytes after x. Also, padding is required
// sizeof(double) = 8
at the end for data alignment.
double z;
// sizeof(short int) = 2
short int y;
// Padding of 6 bytes
};
printf("Size of struct: %ld", sizeof(struct A));
return 0;
}
Size of struct: 24
12
03/24/2020
(b) Example 2
#include <stdio.h>
int main()
{
struct B { The members of the structure are
// sizeof(double) = 8 sorted in decreasing order of their
double z; sizes. Hence padding is required
// sizeof(int) = 4 only at the end.
int x;
// sizeof(short int) = 2
short int y;
// Padding of 2 bytes
};
printf("Size of struct: %ld", sizeof(struct B));
return 0;
}
Size of struct: 16
(c) Example 3
#include <stdio.h>
int main()
{
struct C { Here, y (short int) is followed by x
// sizeof(double) = 8 (int) and hence padding is
double z; required after y. No padding is
// sizeof(short int) = 2 required at the end.
short int y;
// Padding of 2 bytes
// sizeof(int) = 4
int x;
};
printf("Size of struct: %ld", sizeof(struct B));
return 0;
}
Size of struct: 16
13
03/24/2020
Exercise Problems
1. Extend the complex number program to include functions for addition,
subtraction, multiplication, and division.
2. Define a structure for representing a point in two-dimensional Cartesian co-
ordinate system.
• Write a function to compute the distance between two given points.
• Write a function to compute the middle point of the line segment
joining two given points.
• Write a function to compute the area of a triangle, given the co-
ordinates of its three vertices.
3. Define a structure to represent students’ information (name, roll number,
cgpa). Read the data corresponding to N students in a structure array, and
find out the students with the highest and lowest cgpa values.
14