Using a typedef
simplifies struct declaration.
Here is how a struct is declared and used without using typedef
:
struct Emp { int id; double pay; }; struct Emp anEmployee;
and here is how the same is done using a typedef
:
typedef struct { int id; double pay; } Emp; Emp anEmployee;
The identifier Emp
can now be used as the name
of a type without the need to put struct
in front
of it.
Modify the following program
(the answer to puzzle G03) so that it uses
a typedef
and uses the type name where needed.
#include <stdio.h> struct Bulb { int watts; int lumens; }; /* function to print a Bulb */ void printBulb( struct Bulb b ) { printf("watts = %d\tlumens = %d\n", b.watts, b.lumens ); } int main(int argc, char *argv[]) { /* declare and initialize two Bulbs */ struct Bulb bulbA, bulbB; bulbA.watts = 100; bulbA.lumens = 1710; bulbB.watts = 60; bulbB.lumens = 1065; /* print values of both Bulbs */ printBulb( bulbA ); printBulb( bulbB ); system("pause"); return 0; }
The type name can also be used as the type of a function parameter.
Define a struct type, using typedef
,
for a beverage cup.
Cups may be intended for hot or cold beverages,
and contain an integer number of fluid ounces
or mililiters.
Use an enum
with a typedef
for the hot/cold member
and for the oz/ml
member.
If you don't know enum
, use
older-style #define
or integer constants.
/* declare the enums */ /* declare the Cup type */ /* function to print a Cup */ void printCup( Cup *cup ) {...} int main(int argc, char *argv[]) { /* declare and initialize two Cups */ Cup cupA, cupB; /* print values of both Cups */ printCup( cupA ); printCup( cupA ); system("pause"); return 0; }
Define a struct type, using typedef
,
for a date.
A date consists of a month, a day name, and
an integer from 0 to 31.
Use an enum
with a typedef
for the month
and for the day name.
Write a function to set the values of a date and to print a date. Do some easy error checking in the set function, but don't bother writing error messages or returning an error code.
/* declare the enums */ /* declare the Date type */ /* function to print a Date */ void printDate( Date *date ) {...} /* function to set a Date */ void setDate( Date *date ) {...} int main(int argc, char *argv[]) { /* declare and initialize two Dates */ Date birthday, duedate; /* set the dates */ setDate( &birthday, 22, wed, feb ); setDate( &duedate, 13, fri, aug ); /* print values of both Dates */ printDate( birthday ); printDate( duedate ); system("pause"); return 0; }
Define a struct type, using typedef
,
for a color in computer graphics.
Colors are a triple of red, green, and blue
light intensities.
Each intensity may be from 0 to 255.
(But the struct definition itself can't check ranges.)
Write a function that sets the color values and another function to print a color.
/* declare the Color type */ /* function to print a Color */ void printColor( Color *c ) {...} /* function to construct a Color */ void setColor( Color *c, int r, int g, int b ) {...} int main(int argc, char *argv[]) { /* declare and initialize two Colors */ Color grass, sky; setColor( &grass, 50, 200, 100 ); setColor( &sky, 100, 100, 250 ); /* print values */ printColor( &grass ); printColor( &sky ); system("pause"); return 0; }
Define a struct type, using typedef
,
for a point in 2D computer graphics.
A point consists of an integer pair, (X, Y)
for a point in the 2D plane.
Values can be negative, zero, or positive integer.
/* declare the Point type */ /* function to print a Point */ void printPoint( Point *p ) {...} int main(int argc, char *argv[]) { /* declare and initialize two Points */ Point p1={-32,77}, p2={345, 490}; /* print values */ printPoint( &p1 ); printPoint( &p2 ); system("pause"); return 0; }
A struct can contain any data type,
including arrays or other structs.
Define a struct type, using typedef
,
for a triangle in 2D computer graphics.
A triagle consists of three Points
and a Color.
Use three separate members for each of the Points of the triangle. Another possibility is to use an array of three Points, but this is perhaps more bother than it is worth.
/* declare the Point type */ /* declare the Color type */ /* declare the Triangle type */ /* function to print a Point */ void printPoint( Point *p ) {...} /* function to print a Color */ void printColor( Color *c ) {...} /* function to print a Triangle */ void printTriangle( Triangle *t ) {...} int main(int argc, char *argv[]) { /* declare and initialize three Points and a Color */ Point p0={-32,77}, p1={345, 490}, p2={140, 389}; Color c={230, 120, 54}; /* declare and initialize a Triangle */ Triangle tri = {p0, p1, p2, c}; /* print the Triangle */ printTriangle( &tri); system("pause"); return 0; }
Add to the previous program by defining a function that initializes a triangle.
/* declare the Point type */ /* declare the Color type */ /* declare the Triangle type */ /* function to print a Point */ void printPoint( Point *p ) {...} /* function to print a Color */ void printColor( Color *c ) {...} /* function to print a Triangle */ void printTriangle( Triangle *t ) {...} /* function to initialize a Triangle */ void setTriangle( Triangle *t, Point p0, Point p1, Point p2, Color c ) {...} int main(int argc, char *argv[]) { /* declare and initialize three Points and a Color */ Point p0={-32,77}, p1={345, 490}, p2={140, 389}; Color c={230, 120, 54}; /* declare and initialize a Triangle */ Triangle tri; setTriangle( &tri, p0, p1, p2, c ); /* print the Triangle */ printTriangle( &tri); system("pause"); return 0; }
The triangle constructor uses a mix of arguments: some are addresses and others are copies of other structs.
If a triangle is a right triangle (if it has a 90 degree angle) then the square of one side of the triangle is equal to the sum of squares of other two sides. Of course, it is not clear which role the various sides of our triangle play, so all possiblilities have to be tested.
Say that point A = (Xa,Ya) and point B = (Xb,Yb). Then the squared distance between the two points is (Xa-Xb)2 + (Ya-Yb)2. Calculate the squared distance between all three pairs of points and add them in various combinations to determine if the triangle is a right triangle.
/* declare the Triangle type */ void setTriangle( Triangle *t, Point p0, Point p1, Point p2, Color c ) {...} int isRightTriangle( Triangle *t ) {...} int main(int argc, char *argv[]) { /* declare and initialize Points and a Color */ Point p0={0,0}, p1={4, 0}, p2={4, 3}; Point p3={100,50}, p4={500, 350}, p5={212, 434}; Color c={100, 100, 100}; /* declare and initialize a Triangle */ Triangle tri; setTriangle( &tri, p0, p1, p2, c ); printTriangle( &tri ); /* Is is Right? */ if ( isRightTriangle( &tri) ) printf("Right Triangle\n"); else printf("Ordinary Triangle\n"); /* declare and initialize a Triangle */ setTriangle( &tri, p3, p4, p5, c ); printTriangle( &tri ); /* Is is Right? */ if ( isRightTriangle( &tri) ) printf("Right Triangle\n"); else printf("Ordinary Triangle\n"); system("pause"); return 0; }
Write a function that determines if two Point
s
are equal.
Two Point
s are equal if their X coordinate is the
same and their Y coordinate is the same.
/* declare the Point type */ typedef struct { int x, y; } Point; /* function to print a Point */ void printPoint( Point *p ) { printf("(%d, %d) ", p->x, p->y ); } /* are two Points equal? */ int equalPoint( Point *ptA, Point *ptB ) { . . . } int main(int argc, char *argv[]) { Point p1 = { 23, 45 }; Point p2 = { 23, 45 }; Point p3 = { 86, 99 }; if ( equalPoint( &p1, &p2 ) ) printf("Equal: " ); else printf("Not Equal: "); printPoint( &p1 ); printf("\t"); printPoint( &p2 ); printf("\n"); if ( equalPoint( &p1, &p3 ) ) printf("Equal: " ); else printf("Not Equal: " ); printPoint( &p1 ); printf("\t"); printPoint( &p3 ); printf("\n"); system("pause"); return 0; }
Write a function that determines if two Triangle
s
are equal.
Two Triangles
s are equal if
they each have the same three points.
But the points in one triangle might be listed in
a different order than the other triangle,
so the equality function may take some work.
Ignore color in determining triangle equality.
/* declare the Point type */ typedef struct { int x, y; } Point; void printPoint( Point *p ){} int equalPoint( Point *ptA, Point *ptB ){} /* declare the Triangle type */ typedef struct { Point p0, p1, p2; Color color; } Triangle; void setTriangle( Triangle *t, Point p0, Point p1, Point p2, Color c ){} void printTriangle( Triangle *t ){} int equalTriangle( Triangle *a, Triangle *b ){} int main(int argc, char *argv[]) { Point p1 = { 12, 45 }; Point p2 = { 12, 92 }; Point p3 = { 6, 56 }; Color c1 = {255, 123, 100 }; Color c2 = { 90, 3, 133 }; Triangle tA, tB; setTriangle( &tA, p1, p2, p3, c1 ); setTriangle( &tA, p2, p1, p3, c2 ); if ( equalTriangle( &tA, &tB ) ) printf("Equal: " ); else printf("Not Equal: "); printTriangle( &tA ); printf("\t"); printTriangle( &tB ); printf("\n"); system("pause"); return 0; }
This may take some work, because for each point in one triangle you need to find a matching point among the points in the other triangle. But a given point in other triangle may be used to match just one point in the first triangle. The problem is easier if you assume that each point of a triangle is unique, but this might not be the case.