esc101-lec24
TRANSCRIPT
-
7/31/2019 esc101-lec24
1/8
1
ESc101: Fundamentals of Com utin
2011-12-Monsoon Semester
Lecture #24, September 27, 2011
Please switch off your mobile phones.
Announcements
Last date for course dro is 20th October.
I will sign drop requests till 17th October.
Lec-24 1Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
-
7/31/2019 esc101-lec24
2/8
2
Recap
Multi-dimensional arrays
Array of pointers
Dynamic memory allocation
File input/output
Lec-24 2Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
Structures
Structures are customized data types
It is declared using the keyword struct
struct point
{
double x;
double y;
}
struct point is a structure having two variables x and y
Variables in a structure are called members
struct point p;
Structures can be initialized during declaration
struct point p = {4.0, -3.0};
Lec-24 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
3
-
7/31/2019 esc101-lec24
3/8
3
Members
Members of a structure can be explicitly assigned values
. notation to access members
structure variable.member name_ _
p.x = 4.0;
p.y = -3.0
Members behave just like ordinary variables
Size of a structure is the combined size of its members
A bit more complex could be more than this
Example: Size ofstruct point is 8 + 8 = 1 bytes
Lec-24 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
4
Functions returning pointers
A function can return a structure
struct point create_point (float x, float y)
{ struct point p;
p.x = x;
p.y = y;
return p;
}
This can be used to initialize structures
q = create_point (4.0, -3.0);
opy ng can a so e one s mp y y: q = p;
A structure is different from array!
Structures cannot be compared
if (q == p) // error
Lec-24 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
5
-
7/31/2019 esc101-lec24
4/8
4
Passing structures to functions
Since structures are variables, they can be passed to functions
void modify (struct point p, double c, double d)
{
p.x = c;
p.y = d;
}
The following code prints 5.0 and -3.0
struct point q = {5.0, -3.0};
modify (q, 9.0, 1.0);
printf(%lf %lf\n, q.x, q.y);
Lec-24 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
6
Pointers to structures
A pointer to a structure can be defined
struct point *ptr, p; ptr = &p;
When a pointer to structure is passed to a function, modifying the
elements of the structure inside the function becomes ermanent
void modify (struct point *p, double c, double d)
{ p->x = c;
p->y = d;
}
-- > notation to access members using pointers
structure_pointer->member_name
*- .
The following code prints 9.0 and 1.0
struct point q = {5.0, -3.0};
modify (&q, 9.0, 1.0);
printf(%lf %lf\n, q.x, q.y);
Lec-24 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
7
-
7/31/2019 esc101-lec24
5/8
5
Structure operations 1
#include
#include
struct point {
double y;
}; // defining a structure
struct point new_point (double c, double d) // structure as return value
{ struct point p;
p.x = c;
p.y = d;
}
double distance (struct point a, struct point b) // structure as parameter
{ return (sqrt (pow (a.x b.x, 2) + pow (a.y b.y), 2));
}
Lec-24 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
8
Structure operations 2
void modify_wrong (struct point p, double c, double d)
{ p.x = c;
p.y = d;
} // modifications are temporary
vo mo y_r ght (struct po nt p, ouble c, ouble
{ p->x = c;
p->y = d;
} // modifications are permanentint main ( )
{ struct point p, q, s; // declaring structure variables
struct point t = {9.0, -5.0}; // initialization during declaration
double d;
struct point *ptr;
printf(%lf %lf\n, t.x, t.y); // Accessing individual members
q.x = 4.0; // modifying members in structure notation
q.y = -3.0;
Lec-24 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
9
-
7/31/2019 esc101-lec24
6/8
6
Structure operations 3
p = new_point (7.0, -1.0); // Setting structure through functions return value
d = distance (p, q); // passing structures as parameters
printf(Distance = %lf\n, d);
// s = {6.0, 2.0}; // error
modify_wrong (p, 12.0, 8.0);
printf(%lf %lf\n, p.x, p.y); // No modification would have happened
ptr = &p;
modify_right (ptr, 12.0, 8.0); // Now p would have been modified
printf(%lf %lf %lf %lf\n, p.x, p.y, ptr->x, ptr->y);
// if (q == p) // error
}
Lec-24 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
10
Nested structures
A structure can have another structure as its member
struct line {
struct point p;
struct point q;
};
struct line l;
Value x of point p of variable l of type struct line can be
accessed as:
l.p.x
The . operator has left-to-right associativity
Lec-24 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
11
-
7/31/2019 esc101-lec24
7/8
7
Array of structures
struct point t [3];
An individual structure is accessed as t[0], etc.
A member of a structure is accessed as t[1].x, etc.
All operations allowed on normal arrays are allowed on
arrays of structures
Just like any other array, it is equivalent to a pointer tostructure
Lec-24 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
12
Pointer in a structure
A structure can have a pointer as its member
struct student {
int roll;
char *name;
};
struct student s;
Declaring a variable of type struct student just declares the pointer
name it does not allocate space for it
strc (s.name Sudhir // wron
Memory for name has to be allocated explicitly using malloc
s.name = (char *) malloc (30 * sizeof(char));
strcpy (s.name, Sudhir); // right
Lec-24 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
13
-
7/31/2019 esc101-lec24
8/8
8
Any Questions?
Lec-24 14Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon