cd cs31/weeklylab pwd mkdir week11 ls cd week11 pwd cp ~newhall/public/cs31/week11/* . ls
In C, a string is an array of char with a special terminating null character '\0' that signifies the end of the string. The array of chars can be statically or dynamically allocated (by calling malloc). One thing to remember is to allocate enough space for the terminating null character.
Let's take a look at this code and see what it is doing. Note its uses the ctype and string library functions. C string library functions assume that the caller has allocated space for the result string (note the call to strcpy).
example string library functions: strcmp, strlen, strcpy, strchr, example ctype library functions: isalnum, isdigit, isspace
This code also shows an example of using the readline library to read in a string entered by the user. I have some documentation about the readline library here: using the C readline library. The call to readline returns a string (allocated in heap space) to the caller containing the contents of the input line. It is the caller's responsibility to free this returned string.
See my C documentation: char in C, strings in C and Part 1 of intro to C for CS31 students for more information. Also, look at man pages for ctype and string library functions to learn more about how to use them.
Try running with some different input strings, for example:
hello 1 2 3 hello 1 2 3 !@ hello x%
Make the substr pointer point to the begining of the first token and try prining it out. One way to set a pointer to point to any bucket in an array is:
ptr = &(array[i]);
Compile and try running with different input strings and see if it works.
Let's look at the file ptrarth.c. This code contains some examples of using a pointer to iterate over arrays of char and arrays of ints. It uses pointer arithmetic to adjust the pointer at each step to point to the next bucket. Adding a value (lets say 3) to a pointer, does not add three to the address value stored in the pointer variable. Instead, it adjusts the pointer the pointer variable to have the address of the the valid storage location of the type it points to that is three locations beyond it. For example:
int array[10], *ptr, *start, num; ptr = array; start = ptr; *ptr = 6; // puts 6 in bucket 0 of the array ptr = ptr + 3; // make ptr point to bucket 3 of the array // which is at address 12 beyond the current value of ptr *ptr = 8; // puts 8 in bucket 3 of the array num = ptr - start; //In the example code, using pointer arithmetic is not necessary (the same functionality can be accomplished without using pointer arithmetic), but it is handy to use in some cases. Try out the TODO item in this file and run and test it.