Вы находитесь на странице: 1из 8

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #24, September 27, 2011

Please switch off your mobile phones.

Announcements
Last date for course drop is 20th October. p
I will sign drop requests till 17th October.

Lec-24

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Multi-dimensional arrays y Array of pointers Dynamic memory allocation File input/output

Lec-24

Dheeraj 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 A variable of the type structure can be defined using
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

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

E Example: Size of struct point is 8 + 8 = 16 bytes l Si f i i b

Lec-24

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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); C Copying can also be done simply by: i l b d i l b q = p; A structure is different from array! Structures cannot be compared
if (q == p)
Lec-24

// error
Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 5

Passing structures to functions

Since structures are variables, they can be passed to functions Modifying the members inside a function is temporary void modify (struct point p, double c, double d) { p.x = c; p.y = d; } The following code prints 5.0 and -3.0 g p 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

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 permanent p
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

ptr >x is same as (*ptr).x ptr->x (*ptr) x 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

Structure operations 1
#include <stdio.h> #include <math.h> struct point { double x; 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; return p; } 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 void modify_right ( t t point *p, d bl c, d bl d) id dif i ht (struct i t * double double { p->x = c; p->y = d; } // modifications are permanent int main ( ) { struct point p, q, s; // declaring structure variables struct point t = {9.0, -5.0}; // initialization during declaration double d; d bl 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

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}; modify_wrong (p, 12.0, 8.0); printf (%lf %lf\n, p.x, p.y); // error

// 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) }
Lec-24

// error

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

Array of structures
An array of structures can be declared as: 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 to structure

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
strcpy (s.name, Sudhir); Sudhir ); // wrong

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

Any Questions?

Lec-24

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

14

Вам также может понравиться