-
String Examples
This covers ordinary C strings. In C a string is given by a variable
holding the address of the first char of the string. The sequence of
characters including the first must be terminated by a null char '\0'.
(The first char can be a null char, in case of an empty string of length 0.)
The link above shows a number of ways to declare strings and to print them,
including the use of pointer arithmetic.
-
String Basics
This page was adapted from a page written by Mike Maltrud. It contains
some stuff presented elsewhere and some stuff not covered in class,
but it should be helpful. Presents standard C string functions.
Less emphasis on this on the quiz.
-
Functions
Two ways to make a copy of a string, and two ways to compare two
strings.
-
Print Array of Strings
This deals with an array of strings, or what is the same thing,
an array of pointers to char. Such an array is usually terminated
with a NULL pointer. The first program uses the curley bracket
notation to create an array of strings, and then gives a variety
of ways to print them out. Study this carefully.
The second pair of programs gives two other ways to create an
array of strings. In the first, the array of pointers is declared
as local storage. In the second the array of pointers is allocated
using malloc.
-
2d-arrays
This is a formidable page, but I hope it will prove helpful.
Basically, this shows the two ways in C to declare a 2-dimensional array.
On the left is an "ordinary" C 2-dimensional array (not available in Java), which is just stored
inside a 1-dimensional array, and array references are faked by the
compiler using a formula.
The second method is the only one available in Java: an array of pointers is
created (as local storage or using malloc).
Then each of these pointers is given the address of an array of the
type of the 2-dimensional array. This method is fundamentally different
from the first.
In each case there is a "natural" method to express an array element,
but it is interesting that almost all methods work for both types of
2-dimensional array. (Only the tricky formula doesn't work for the array
of pointers to arrays.)
-
Parameters
This weird page is mostly to loosen you up. Here on the left and middle,
the address of
an array of strings is passed as a parameter, of type char ***.
In the leftmost example, a pointer to local (stack) storage is provided back
to the main function. This is always an error.
In the middle program, a char *** is passed, and what
it points to is an array of strings created in the function called.
The third example (on the right) is significantly different. Here storage
for the array of strings is allocated in the main function.
Then just a char ** (TWO stars, not THREE) is passed,
but it gives the address of an already allocated array of pointers to char.
These pointers are filled in with strings, and the result is available
in the main function.