Académique Documents
Professionnel Documents
Culture Documents
Computing
Structures
Motivation
array of size 2?
we keep two variables point_x and point_y?
(But there is no way to indicate that they
are part of the same point! - requires a
disciplined use of variable names)
Structures
A structure is a collection of variables with a common
name.
The variables can be of different types (including
arrays, pointers or structures themselves!).
Structure variables are called fields.
struct point {
int x;
int y;
};
struct point pt;
pt
memory depiction of pt
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;
int y;
};
struct point pt;
pt.x = 0;
pt.y = 1;
pt
memory depiction of pt
Structures
struct point {
int x; int y;
}
For now,
define structs
in the
beginning of
the file, after
#include.
pts[2]
pts[3]
pts[4]
pts[5]
pts[0] pts[1]
int i;
for (i=0; i < 6; i=i+1) {
pts[i].x = i;
pts[i].y = i;
}
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.
pts
pts[0] pts[1]
pts[2]
pts[3]
pts[4]
5
5
pts[5]
struct point {
Functions returning structures
int x; int y;
};
1.
make_point(x,y) creates
a struct point given
struct point make_point
coordinates (x,y).
(int x, int y)
{
2.
Note: make_point(x,y)
struct point temp;
returns struct point.
temp.x = x;
temp.y = y;
3.
Functions can return
return temp;
structures just like int,
}
char, int *, etc..
int main() {
int x, y;
4.
We can also pass struct
struct point pt;
parameters. struct are
scanf(%d%d, &x,&y);
passed by copying the
pt = make_point(x,y);
values.
return 0;
}
Given int coordinates x,y, make_point(x,y) creates
and returns a struct point with these coordinates.
x2 y2
norm2(struct point p)
returns Euclidean norm of
point p.
leftbot
righttop
1.
2.
3.
struct point {
r.leftbot.y
r.righttop.y
int x;
r.leftbot.x
r.righttop.x
int y;
};
r
leftbot
righttop
struct rect {
struct point leftbot;
x 0
x 1
struct point righttop;
};
y 0
y 1
int main() {
rect r;
r.leftbot.x = 0;
r.leftbot.y = 0;
r.righttop.x = 1;
r.righttop.y = 1;
return 0;
}
Addressing nested fields
unambiguously
Initializing structures
struct point {
int x; int y;
};
1.
2.
3.
struct rect {
struct point leftbot;
struct point righttop;
};
struct point p = {0,0};
struct point q = {1,1};
struct rect r = {{0,0}, {1,1}};
(1,1)
q
r
p (0,0)
leftbot
righttop
x
y
1.
2.
leftbot
righttop
leftbot
righttop
1.
2.
leftbot
righttop
leftbot
righttop
Passing structures..?
We can pass structures
as parameters, and
return structures from
functions, like the
basic types int, char,
double etc..
But is it efficient to
pass structures or to
return structures?
So what should
y
be done to pass
structures to
Same for returning structures functions?
y
leftbot
righttop
Passing structures..?
Instead of passing
structures, pass
pointers to structures.
area() uses a pointer
to struct rect pr as a
parameter, instead of
struct rect itself.
struct point {
int x; int y;};
struct rect {
struct point leftbot;
struct point righttop;
};
struct rect *pr;
1.
2.
3.
4.
Structure Pointers
(*pr).leftbot.y
(*pr).righttop.y
(*pr).leftbot.x (*pr).righttop.x
pr
leftbot
righttop
Addressing fields
via the structures
pointer
struct point {
int x; int y;};
struct rect {
struct point leftbot;
struct point righttop;
};
struct rect *pr;
1.
2.
3.
3.
Structure Pointers
pr->leftbot.y
pr->righttop.y
pr->leftbot.x
pr
pr->righttop.x
leftbot
righttop
pts[0] pts[1]
pts[2]
pts[3]
pts[4]
pts[5]
More on typedef