0% found this document useful (0 votes)
19 views23 pages

Lec 28

Uploaded by

hrshv941
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views23 pages

Lec 28

Uploaded by

hrshv941
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Composite Data Types:

Structures
ESC101: Fundamentals of Computing
Nisheeth
Composite Data

Case 1: A geometry package – we want to define a variable for a two-
dimensiona point to store its x coordinate and y coordinate.

Case 2: Student data – Name and Roll Number

First strategy: Array of size 2?

Will work for case 1 but not for case 2 since we can not mix TYPES

Another strategy: Use two variables,
int point_x , point_y ; char *name; int roll_num;

No way to indicate that both variables are part of the same “big” variable

We need to be very careful about variable names.

Is there any better way ?

2
Structures
• A structure is a collection of variables under a common name.
• The variables can be of different types (including arrays, pointers or structures
themselves!).
• Each variable within a structure is called a field.
name of this
structure

struct point { Defines a structure named point


int x; containing two integer variables (fields),
int y; called x and y.
}; struct point pt; defines a variable pt to
struct point pt; be of type struct point.

pt x
memory depiction of pt
y 3
Structures
• The x field of pt is accessed as pt.x.
• Field pt.x is an int and can be used as any other int.
• Similarly the y field of pt is accessed as pt.y

struct point {
int x; pt
int y; x 0 memory depiction of pt
};
struct point pt; y 1
pt.x = 0;
pt.y = 1;
4
Structures struct point {
int x; int y;
}

struct point is a type. For now, define structs in the


It can be used just like int, beginning of the file, after #include.
char etc..

We can even define an struct point pt1,pt2;


array of struct point struct point pts[6];
pts

x x x x x x

y y y y y y
pts[0] pts[1] pts[2] pts[3] pts[4] pts[5]
5
pts

x x x x x x

y y y y y y
pts[0] pts[1] pts[2] pts[3] pts[4] pts[5]

int i;
for (i=0; i < 6; i=i+1) { Read pts[i].x as (pts[i]).x
pts[i].x = i; The . and [] operators have same
pts[i].y = i; precedence. Associativity: left-right.
}

6
Structures
struct point {
int x; int y;
};
struct point pts[6];
int i;
for (i=0; i < 6; i=i+1) {
pts[i].x = i;
pts[i].y = i;
}
State of memory after the code
executes.

x 0 x 1 x 2 x 3 x 4 x 5
pts
y 0 1 y 2 3 y 4 5
y y y
pts[0] pts[1] pts[2] pts[3] pts[4] pts[5]
7
struct point { Reading structures (scanf ?)
int x; int y;
};
int main() {
int x, y;
struct point pt;
scanf(“%d%d”, &(pt.x),&(pt.y));
return 0;
}

1. You can not read a structure directly using scanf!

2. Read individual fields using scanf (note the &).

3. A better way is to define our own functions to read structures



to avoid cluttering the code!

8
struct point { Functions returning structures
int x; int y;
};
make_pt(x,y):
struct point make_pt(int x, int y) { creates a struct point with
struct point temp; coordinates (x,y), and
temp.x = x; returns a struct point.
temp.y = y;
return temp; Functions can return
} structures just like int, char,
int main() { int *, etc..
int x, y;
struct point pt; struct can be passed as
scanf(“%d%d”, &x,&y); arguments (pass by value).
pt = make_pt(x,y);
return 0;
}
Given int coordinates x,y, make_pt(x,y) creates and returns a
struct point with these coordinates. 9
Functions with structures as parameters
# include <stdio.h>
# include <math.h> The norm2 or Euclidean norm
struct point { of point (x,y) is
int x; int y;
}; x2  y2
double norm2( struct point p) {
return sqrt ( p.x*p.x + p.y*p.y); norm2(struct point p) returns
} Euclidean norm of point p.
int main() {
int x, y;
struct point pt;
scanf(“%d%d”, &x,&y);
pt = make_point(x,y);
printf(“distance from origin
is %f ”, norm2(pt) );
return 0;
}
10
Structures inside structures
struct point { 1. Recall, a structure definition defines a type.
int x; int y; 2. Once a type is defined, it can be used in the
}; definition of new types.
3. struct point is used to define struct rect. Each
struct rect { struct rect has two instances of struct point.
struct point leftbot;
struct point righttop;
};
struct rect r;

r r is a variable of type struct rect. It has


leftbot righttop
two struct point structures as fields.
x x
So how do we refer to
y y the x of leftbot point
structure of r?
11
struct point {
r.leftbot.y r.righttop.y
int x;
int y; r.leftbot.x r.righttop.x
};
struct rect { r leftbot righttop
struct point leftbot;
struct point righttop; x 0 x 1
};
int main() { y 0 y 1
struct rect r;
r.leftbot.x = 0;
r.leftbot.y = 0;
r.righttop.x = 1;
r.righttop.y = 1;
return 0;
}
Addressing nested fields
unambiguously
12
Initializing structures
struct point {
int x; int y;
};
1. Initializing structures is very similar
to initializing arrays.
2. Enclose the values of all the fields (1,1)
in braces. q
3. Values of different fields are
separated by commas. r

struct rect {
struct point leftbot; p (0,0)
struct point righttop;
};
struct point p = {0,0};
struct point q = {1,1};
struct rect r = {{0,0}, {1,1}};
13
Assigning structure variables
struct rect r,s; 1. We can assign a structure variable to
r.leftbot.x = 0; another structure variable
r.leftbot.y = 0; 2. The statement s=r; does this
r.righttop.x = 1; 3. Structures are assignable variables,
r.righttop.y = 1; unlike arrays!
s=r;

s r
leftbot righttop leftbot righttop

x x x 0 x 1

y y y 0 y 1

Before the assignment 14


Assigning structure variables
struct rect r,s; 1. We can assign a structure variable to
r.leftbot.x = 0; another structure variable
r.leftbot.y = 0; 2. The statement s=r; does this.
r.righttop.x = 1; 3. Structures are assignable variables,
r.righttop.y = 1; unlike arrays!
s=r;

s r
leftbot righttop leftbot righttop

x 0 x 1 x 0 x 1

y 0 y 1 y 0 y 1

After the assignment 15


Assigning structure variables
struct rect r,s; 1. We can assign a structure variable to
r.leftbot.x = 0; another structure variable
r.leftbot.y = 0; 2. The statement s=r; does this.
r.righttop.x = 1; 3. Structures are assignable variables,
r.righttop.y = 1; unlike arrays!
s=r; 4. Structure name is not a pointer, unlike
arrays.

s r
leftbot righttop leftbot righttop

x 0 x 1 x 0 x 1

y 0 y 1 y 0 y 1

After the assignment 16


struct rect { struct point leftbot; Passing structures..?
struct point righttop; }; We can pass structures as
int area(struct rect r) {
parameters, and return
return
structures from functions,
(r.righttop.x – r.leftbot.x) *
(r.righttop.y – r.leftbot.y); like the basic types int,
} char, double etc..
void fun() {
int ar;
struct rect r1 ={{0,0}, {1,1}};
But is it efficient to pass
ar = area(r1); structures or to return
} structures?
r leftbot righttop Usually NO. E.g., to pass struct rect as
parameter, 4 integers are copied. This is
x expensive.
x

y So what should be
y
done to pass
structures to
Same for returning structures functions? 17
struct rect { struct point leftbot; Passing structures..?
struct point righttop;};
int area(struct rect *pr) { Instead of passing
return structures, pass pointers to
((*pr).righttop.x – (*pr).leftbot.x) * structures.
((*pr).righttop.y – (*pr).leftbot.y);
}
void fun() { area() uses a pointer to
int ar; struct as a parameter,
struct rect r ={{0,0}, {1,1}}; instead of struct rect itself.
ar = area (&r);
}

Only one pointer instead of


large struct.

Same for returning structures


Esc101, Structures 18
struct point {
int x; int y; Structure Pointers
}; (*pr).righttop.y
struct rect { (*pr).leftbot.y
struct point leftbot; (*pr).leftbot.x (*pr).righttop.x
struct point righttop;
pr
}; leftbot righttop
struct rect *pr;
x 0 x 1
1. pr is pointer to struct rect.
2. To access a field of the struct y 0 1
y
pointed to by struct rect, use
(*pr).leftbot
(*pr).righttop
3. Bracketing (*pr) is essential here. * Addressing fields
has lower precedence than . via the structure’s pointer
4. To access the x field of leftbot, use
(*pr).leftbot.x
19
Addressing fields via the pointer (shorthand)
pr->leftbot.y pr->righttop.y

pr->leftbot.x
pr->righttop.x
pr
leftbot righttop

x 0 x 1
1. Shorthand: arrow operator(->) is
provided. y 0 y 1
2. To access a field of the struct , use
pr->leftbot
3. -> is one operator. To access x field pr->leftbot is equivalent
to (*pr).leftbot
of leftbot, pr->leftbot.x
4. -> and . have same precedence
and are left-associative. Equivalent
to (pr->leftbot).x
20
Passing by value or reference

• When a struct is passed directly, it is passed by copying its


contents
– Any changes made inside the called function are lost on return
– This is same as that for simple variables
• When a struct is passed using pointer
– Change made to the contents using pointer dereference are visible
outside the called function

Esc101, Structures 21
Functions Returning Structures
struct point make_pt (int x, int y) { struct point {
struct point temp; int x; int y;
temp.x = x; };
temp.y = y;
return temp; }

void print_pt (struct point pt) {


printf(“%d %d\n”, pt.x, pt.y); }

int main() {
int x, y;
struct point pt;
scanf(“%d%d”, &x,&y);
pt = make_pt(x,y);
print_pt (pt);
return 0; } 22
Even though not
returning anything,
make_pt is still able
to do the job using
Functions Returning Structures
pointers

void make_pt(int x, int y, struct point *temp) { struct point {


temp->x = x; int x; int y;
temp->y = y; };
}

void print_pt(struct point *pt) {


printf("%d %d\n", pt->x, pt->y);
}

int main() {
int x, y;
struct point pt;
scanf("%d%d", &x,&y);
make_pt(x,y, &pt);
print_pt(&pt);
return 0;
} 23

You might also like