CITS2002 Systems Programming

# Department of Computer Science and Software Engineering

## CITS2002 Systems Programming

### Labsheet 5 - for the week commencing 23rd September 2019

Lecture 15 introduced the concept of pointers, an often difficult topic for new programmers to comprehend. However, the concept is very important and, when used correctly, very powerful.

As always, take the time to understand the underlying concepts before attempting to implement the tasks below. Refer to the lecture notes and a book or tutorial for help. One final piece of advice: never ignore warnings from the compiler about assignments for pointers "without a cast" - this inevitably means you are doing something wrong.

Completion of most exercises will be very beneficial for the unit's project, particularly those marked [PROJECT].

### Exercises

1. [PROJECT] Write a function named money that accepts a single integer parameter that represents a total number of cents, and breaks the total down into its numbers of dollars and cents. Your function should calculate the number of dollars, and the number of "left-over" cents, and provide these to its calling function through its parameters. For example, a value of 524 should be "returned" as 5 dollars and 24 cents.

Your function should have the prototype:

`     void money(int total, int *dollars, int *cents);`

and be called as:

`     money(524, &dollars, &cents);`

2. Using the example strlen() and strcpy() functions of Lecture 15 as a guide, write pairs of functions to implement versions of the following standard C functions:

1. strcat

2. strcmp

3. strncmp

4. strchr

5. strrchr

6. 🌶 strpbrk

7. 🌶 🌶 strstr

Maintain two separate C source files for this task: one file for the array-based versions of each function (named arrays.c), and one file for the pointer-based versions (named pointers.c). Develop two separate header files, one containing the prototypes of the array-based functions (named arrays.h), and the other containing the prototypes of the pointer-based functions (named pointers.h).

Create a single testing program named stringtest.c, containing the only main() function for this question, that tests each of the functions you write. The file stringtest.c should include both of your header files.

Approach the writing of each function by following these steps:

1. Firstly, read the appropriate online Unix manual entry for each function (e.g. run man strcat). Pay particular attention to the parameters of the function and its return value. Note that the online documentation will frequently use the (as yet not explained) keywords const and restrict. For now, simply ignore these and do not reproduce them in your functions (ask a demonstrator if interested in what they're for).

2. Next, develop a version of each function using arrays (not pointers), in arrays.c, As an example, name your first function strcat_a().

3. Write a short test function in stringtest.c file that calls your new function several times to test its execution. Depending on the function being tested, suitable parameters to the function will include simple short strings, long strings, empty strings, identical strings (when comparing them), very different strings, strings differing in only a single character, and strings containing, or otherwise, a required character (for strchr).

4. Next, develop a version of each function using pointers (not arrays) in pointers.c. As an example, name your first function strcat_p(). Undertake the same tests on your pointer-based functions as you did on your array-based ones.

3. [PROJECT] 🌶 Write a filter program named reverse, which prints out the lines of a text file in reverse order - the last line is printed first, and the first line is printed last.
Ensure that your program can operate on files named on the command-line, and from text lines arriving via stdin.

4. [PROJECT] Locate a single-file program consisting of several functions, such as the one developed in Workshop-3 - and its solution haversine.c.

Now, break the single-file program into a multi-file project consisting of:

• a single C header file (providing the function declarations), and
• several C source code files (with each C file providing at least one function definition).

Next, develop a simple Makefile, as introduced in Lecture-12, to compile and link your multi-file project using make.

Determine how to test that your Makefile correctly represents the dependencies between your source and header files.

Extend your Makefile by adding a new target to cleanup (remove) any unnecessary files (careful!).

5. 🌶 [PROJECT] While not too technically challenging, this exercise may take a lot of time (typing) to complete, so you may just wish to think about the problem, or to just sketch out a solution.

Consider the sample solution for the 1st project. As discussed in Lecture 17, the use of many global arrays instead of C's structures, makes the implementation both harder to read and not truly representative of the problem we're modelling.

Identify parts of the program where the use of related arrays and scalar variables could be redesigned (restructured) to employ C's structures. Some likely possibilities include the 2-dimensional arrays holding devices, processes, processes' events, and the Ready and I/O blocked queues.

In some cases the best approach may be to "gather" multiple arrays (indexed by process number) into an array of structures (still indexed by process number); in other cases we may gather together a small number of scalar variables because they are related in their use.

6.  🌶 🌶 Write a function named sortPointers() that sets an array of integer pointers to point to the elements of another array in ascending order. Your function should accept three parameters: the array of integers, the array of integer pointers to assign, and the integer count of the number of elements in the array, and hence should have the prototype: ` void sortPointers(int src[], int *ptrs[], int n);` Your function should not change the original array, nor create any other arrays in completing this task.

# The University of Western Australia

## University information

CRICOS Code: 00126G