struct point
with the following definition:
struct point { double x; double y; };In the following questions, we will refer to the struct through pointers, using
typedef struct point * Point
for convenience.
(*((*speeding[s]).date)).year
Point newPoint(double x, double y)
that creates a new point by initialising its values and uses malloc to dynamically allocate the structure.
void destroyPoint(Point p)
that deletes a point by freeing its underlying memory.
double getX(Point p)
and double getY(Point p)
that returns the corresponding coordinate of a given point.
void setPoint(Point p, double x, double y)
that sets the corresponding coordinates of a given point.
void shiftPoint(Point p, double xDist, double yDist)
that adds given distances to the corresponding coordinates of a point.
Point
typedef in a Point.h
file and the function and struct definitions in Point.c
. Only the main function should be left in your original file, which you can name testPoint.c
(make sure to #include "Point.h"
at the top of each .c
file). How would you now compile this program?
Point p = newPoint(0,0);
shiftPoint(p,1,-1);
printf("x value is: %f\n", getX(p));
printf("y value is: %f\n", getY(p));
destroyPoint(p);
What is the output?
shiftPoint()
to call setPoint()
(or not call it if your implementation already does). Does the output change? Should it?
getX(p)
to p->x
not work?