Exercise 15: Pointers to Structs

For this assignment you will implement a function which calculates the area and perimeter of a rectangle. We will assume that the rectangle’s sides are parallel to the x and y axes, and in this way we can represent a rectangle with 2 diagonally opposite points.

main() contains code that will ask the user for the coordinates of the two points that will define the rectangle, calls the function that you will write, and then prints the area and perimeter.

Often when structs are passed to functions, a pointer is passed to save on copying. Similarly, often instead of returning a struct a function modifies a struct through a pointer in order to “return” the result via pointer. The function that you will write uses both of these techniques.

Here are the structs you will use in this assignment, which are already defined in rectangle.h:

    struct point {
        int x;
        int y;
    };
    
    struct rectangle {
        struct point p1;
        struct point p2;
    };
    
    struct rectangle_properties {
        int area;
        int perimeter;
    };

The p1 and p2 members of struct rectangle represent two points which are diagonally opposite one another in a rectangle. When we looked at this example before, we said that the first point was the upper left point and the second was the lower right. In the struct here, the two points could be any combination of diagonally opposite points (e.g. p1 could be the upper right and p2 the lower left, or vice versa).

The get_rectangle_properties() function will take a pointer to a struct rectangle and a pointer to a struct rectangle_properties as parameters, calculate the area and perimeter of the rectangle pointed to by the struct rectangle pointer, and store the results in the struct pointed to by the struct rectangle_properties pointer.

Here is the prototype:

    /**
     * Given a pointer to a rectangle, calculate the area and perimeter of the
     * rectangle and store the results in the struct pointed to by props.
     *
     * Parameters:
     *   rect - pointer to a rectangle
     *   props - pointer to the struct in which to store the results
     */
    void get_rectangle_properties(const struct rectangle *rect,
                                  struct rectangle_properties *props);

Note that rect is a const struct rectangle *. It is const so that the rectangle remains unchanged. props cannot be const because we need to set the values of its members.

Recall that the syntax for accessing a structure instance’s member via a pointer is instance_pointer->member, so to access the p1 member of rect you would use rect->p1. Since p1 is not a pointer, you will use the . syntax to access the x and y members from there (for example, rect->p1.x).

The library stdlib.h contains a function named abs(). When passed an integer, abs() will return the absolute value of the integer. This makes calculating the width and height easier, since you do not need to check the relative locations of the points.

Local Testing

You can run the GitHub testing locally with the command:

make gh-test-rectangle

Submission

Push your submission to GitHub.

Grading

Your grade for this assignment will be out out of 10 points: