Computer programming - Lab 9

Type casts and void *

1. Print from address array 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.

2. Read to address array 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.

3. Print from buffer Write a function int bufprintf(const char *fmt, void *buf, size_t size) that works like printf, but receives a buffer from which it takes the values to be printed, and the size of that buffer. Values are stored at consecutive bytes in the array.
Implement the format specifiers %d, %f and %s, as well as printing regular characters and %. An error encountered in the format or reaching the end of the buffer should terminate printing.

4. Read into buffer Write a function int bufscanf(const char *fmt, void *buf, size_t size) that works like scanf, but receives a buffer into which it should place values and its size. Values read should be stored at consecutive bytes in the array.
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 or reaching the end of the buffer should terminate reading.

Function parameters

5. Countif 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.

6. Map 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.

7. Partition Write a function that rearranges the elements of an array: first all that satisfy a given property, and then all that don't.
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. For example, you could group elements of an array that are less than / not less than a given value (this partitioning step is used in quicksort).

Recursive string processing

8. Nested tags Write a function that takes as argument a string with XML text and checks that tags are properly nested. An opening tag is <tagname> and a closing tag is </tagname> . There can also be arbitrary text without < and > . Return 1 or 0 and print the error location.
Hint: On each recursion level keep a pointer to the opening tag (and perhaps its length) and check the same name appears in the closing tag.
Marius Minea
Last modified: Wed Nov 25 9:00:00 EET 2015