Computer programming - Lab 9

1. Write a function that reads text from input into a buffer and returns a NULL-terminated array of pointers to lines of the text read.
The function has four parameters: a text buffer and an array of addresses, each with their respective size. The function should store the text read from input into the buffer, storing the start of every line into the address array, and returning when either of the arrays fills up. Read the text directly at the right position into the buffer, avoiding useless copying.

2. Write a function int myprintf(const char *fmt, void *adrtab[]) that works like printf, but receives a NULL-terminated array of addresses (void *) of objects to be printed.
Implement the format specifiers %d, %f and %s, as well as printing regular characters and %. An error encountered in the format should terminate printing.

3. Write a function int myscanf(const char *fmt, void *adrtab[]) that works like scanf, but receives a NULL-terminated array of addresses (void *) of objects to be read.
Implement the format specifiers %d, %lf and %MAXs (where MAX is an unsigned constant) as well as matching regular characters and whitespace. An error encountered in the format should terminate reading.

4. Write a function that counts how many elements in an array satisfy a given condition.
The function takes as parameters an array, its element count, element size, and a pointer to a function. That function takes as parameter a pointer to an element (as void *) and returns nonzero (meaning the element satisfies the desired condition) or zero (it does not). Test your function in a program.

5. Write a function that changes all elements of an array by calling a function.
The function takes as parameters an array, its element count, element size, and a pointer to a function. That function takes as parameter a pointer to an element (as void *) and changes the value of the element. Test your function in a program.

6. Write a function that computes a combined value from all elements of an array. The function takes as parameters an array, its element count, element size, a void * to an initial value and a pointer to a update function. That update function takes a void * to a value to update and a void * to an element and updates the value taking into account the element. The update function should be successively called for each array element. Use the function to compute the sum of an integer array, or to concatenate strings.

7. Write a function which concatenates a string from several pieces. The function takes as parameters a buffer to store the string, its size, and a NULL-terminated array of (pointers to) strings. The array contains alternatively strings to process e.g. "could be anything" and range specifications "9-12" (two numbers separated by a dash). The indicated characters from each string should be copied into the buffer. Use argv[] (starting with the first argument) for the strings to process.


Marius Minea
Last modified: Fri Nov 21 11:00:00 EET 2014