User Tools

Site Tools


cs270:pointadt

Exercise - Testing the Point ADT

Objectives

  • Better understand passing pointers to and returning pointers from functions
  • More examples of pointer variables and the operator
  • Learn how to compile multi-file programs

Problem

In this section you'll work with the files that make up our simple Point ADT (Abstract Data Type). To start, download the files from the webserver

Make sure they compile and run as is. To compile a program made up of multiple files, add each .c files to the gcc command. Also, this program uses sqrt from math.h so we need the -lm flag to pull in the math library. So the gcc command looks like this:

gcc -Wall -o pointTest point.c pointTest.c -lm

  1. Add code to test the initPoint function; this function dynamically allocates space for a Point and initializes it, much as a Java constructor does. The appropriate code will go in the main function in pointTest.c. Note the return value! Once you have the address of this new point, use printPoint to display it.
  2. In point.c, add checks for the case that calls to malloc return NULL. If this happens in initPoint, return the NULL value. If this happens in readPoint, set label to NULL and return before the strcpy, which would otherwise fail.
  3. Since readPoint takes a pointer variable as its argument, it's a good idea to check that it isn't NULL before getting too far into the function. Do this check first thing, and just return straightaway if p is in fact NULL.
  4. Compile and test.
  5. When you have it working, demonstrate to the instructor

As always, if you run out of time or are working remotely, send me an e-mail containing screenshots of the working programs by the end of the day for full credit. Also send the C files point.c and pointTest.c so I can see your NULL checks. This exercise is due before 5pm Friday. Schedule a meeting if you have difficulties.

cs270/pointadt.txt · Last modified: 2020/09/14 13:50 by scarl