Lec 28
Lec 28
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
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;
}
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;
}
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;
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
s r
leftbot righttop leftbot righttop
x 0 x 1 x 0 x 1
y 0 y 1 y 0 y 1
s r
leftbot righttop leftbot righttop
x 0 x 1 x 0 x 1
y 0 y 1 y 0 y 1
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);
}
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
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; }
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
int main() {
int x, y;
struct point pt;
scanf("%d%d", &x,&y);
make_pt(x,y, &pt);
print_pt(&pt);
return 0;
} 23