Professional Documents
Culture Documents
Structures
Henry Chia
hchia@comp.nus.edu.sg
Lecture Outline
Structures.
Structure definition.
Declaration with structures.
Assigning structures.
Accessing members of a structure.
Using functions with structures.
Passing structures by value to a function.
Returning a structure from a function.
Problem solving involving structures.
computeDist
x1
1.0
y1
1.0
x1
1.0
y1
1.0
x2
2.0
y2
2.0
x2
2.0
y2
2.0
distance
1.0,1.0,2.0,2.0
return 1.414..;
1.414..
distance = computeDist(x1,y1,x2,y2);
CS1010E Lecture 6 p.4/24
Structures
Thus far, declaring variables of int and double allow
us to work with individual numeric values.
To work with practical problems, each variable or value
may constitute a set of information/data record
A point (x, y) comprises two floating-point values.
A fraction xy comprises two integers for the
numerator and denominator.
A bank account is associated with an integer
account number, and a floating-point balance.
A structure defines a set of heterogeneous data for a
record, i.e. the individual parts of the data do not have
to be of the same type.
Structure Definition
struct struct_identifier
{
declarations
}; // Note the semi-colon
struct Fraction
{
int num;
int den;
};
struct BankAccount
{
int accountNum;
double balance;
};
Structure Definition
Defining a structure is to define a new type.
struct Point
{
double x, y;
};
Point
Point
Point
Point
pt1={1.2,3.4};
unit={1};
origin={0};
pt2;
//
//
//
//
(1.2,3.4)
(1.0,0.0)
(0.0,0.0)
(?,?)
pt1
1.2
3.4
1.2
3.4
pt2
1.2
3.4
pt2 = pt1;
pt1
pt2
Member Operator
Arithmetic/relational/logical operations cannot
be applied on entire structure variables.
The operations above make sense only when
applied on specific data members.
A data member is referenced using the
structure variable name followed by the
structure member operator (.) and a data
member name.
Example, to compare points pt1 and pt2,
if (fabs(pt1.x - pt2.x) < EPSILON &&
fabs(pt1.y - pt2.y) < EPSILON )
printf("The two points are equal.\n");
CS1010E Lecture 6 p.13/24
Member Operator
Apart from initialization, structure variables
can also be assigned by assigning individual
data members.
pt1.x = 1.2;
pt1.y = 3.4;
Member Operator
For input and output, use scanf to read
values into the data members, and printf to
print their values.
The following reads a point (two floating point
values) as input, and outputs the point by
printing the individual x and y members.
struct Point pt1;
scanf("%lf %lf", &(pt1.x), &(pt1.y));
printf("The point is (%lf,%lf)\n",
pt1.x, pt1.y);
computeDist
pt1
1.0 1.0
pt1
1.0 1.0
pt2
2.0 2.0
pt2
2.0 2.0
{1.0,1.0},{2.0,2.0}
distance
return 1.414..;
1.414..
distance = computeDist(pt1,pt2);
Returning a Structure
A function can be defined to return a single
value of type struct.
After the function is called, the entire structure
content is returned to the calling function.
Write a function midPoint that returns the
middle point between (x1 , y1 ) and (x2 , y2 ).
The midpoint (xm , ym ) is computed as
x 1 + x 2 y1 + y2
(xm , ym ) = (
,
)
2
2
struct midPoint(struct Point pt1,
struct Point pt2);
CS1010E Lecture 6 p.18/24
Returning a Structure
struct Point midPoint(struct
struct
{
struct Point midpt;
midpt.x = (pt1.x + pt2.x)
midpt.y = (pt1.y + pt2.y)
return midpt;
main
}
pt1
pt2
Point pt1,
Point pt2)
/ 2;
/ 2;
midPoint
pt1
1.0 1.0
pt2
2.0 2.0
1.0 1.0
2.0 2.0
{1.0,1.0},{2.0,2.0}
*
midpt
midpt
1.5 1.5
{1.5,1.5}
return midpt;
midpt = midPoint(pt1,pt2);
CS1010E Lecture 6 p.19/24
>
>
>
>
r2.max.x ||
r1.max.x ||
r2.max.y ||
r1.max.y;
return !noOverlap;
}
Lecture Summary
Definition of structures before usage
(declarations, functions, etc.)
Structures behave similarly to primitives.
Structure member operator .
Using functions with structures.
Pass-by-value/return of structures into/from
functions behave similarly to passing
primitives.