In this lab, you will demonstrate a competency in the "C" programming language. Competency in C will be required for future success in this course.
In general, tokenizers partition a character string into individual tokens (words) separated by reserved "delimiter characters".? In this case, the delimiter character is the space character and tokens contain any characters other than space characters.
Your function "mytoc" should have the following signature:
char ** mytoc(char *)
A call to mytoc() should return a freshly allocated null-terminated array of pointers to freshly allocated strings containing each "token" from the input string. The input string can be of any length, and should not be modified. The vector returned by mytoc() and the strings referenced by it, should all be allocated using malloc or calloc (so that it would be appropriate to free() them). Finally, these dynamically allocated blocks of memory should not be larger (or smaller) than required.
For example, a call to mytoc("hello world") should return the address of an array containing:
{"hello", "world", (char*)0}
Other than malloc/calloc/free, your implementation of mytoc should only call functions written by you. In particular, calls to functions whose prototypes are in "string.h" such as "strtok()" are prohibited.
A "C" source file "foo.c" can be compiled to an object file
C is an extremely small language that (for example) provides no I/O, string handling or math functions. Most distributions of C provide this functionality through a rich set function libraries.? For example, the standard library, often called "libc" contains object files "printf.o" and "scanf.o" which implement the familiar functions "printf" and "scanf". To prepare the compiler's "object" file for execution, it must be "linked" with object files providing all referenced functions. By when linking objects into executables, "cc" first includes all object files on the command line, and then searches "libc" for objects defining variables or functions referenced but not defined by these object files.
The make utility is a program that can be told of the dependency of compiled files from their source. For example, in your program:
Output | Input | Command to generate output file |
test | test.o, and mytoc.o | cc -o test test.o mytoc.o |
mytoc.o | mytoc.c | cc -c mytoc.c |
test.o | test.c and mytoc.h | cc -c test.c |
Makefiles (named "Makefile") contain production rules of the form
output file: source filescommand to run if any source file is newer than the output file
By default, make will attempt to build the first output file listed in the Makefile.
Your test program should, in a loop:
I strongly prefer that you use the system call write() rather than
printf() to emit the "$ " prompt. If you use "printf" to print the
string, you will need to call fflush(stdout) to force the prompt to be
printed. Don't forget to "#include
Questions about this lab should be sent to the course mailing list.