Previous Topic

Next Topic

Strings

One of the shortcomings of the C language is that there is no formal data type for character strings. All string manipulation is done by processing arrays of characters. This brings us to another shortcoming of the C language - there is no automatic checking on the boundaries of an array. These features can be exploited in many cases, but when it comes to string manipulation they create a major problem.

When you are copying a character string from one variable to another, it is up to the programmer to ensure that the receiving variable has enough room to hold the entire string being copied. If you are not careful, it is possible for the string being copied to overlay an adjacent variable in addition to the target, with unpredictable results. This may be the most common error that can occur in a C program, and it can be very difficult to trace.

To complicate matters, many string manipulation operations performed in a c-tree Plus program are NOT working with traditional null-terminated character strings. As we will see later, some types of c-tree Plus key values may have null characters embedded in the middle. In addition, copying data structures from one location to another is handled as a string manipulation, and the data structure will have a number of null characters embedded in it.

To assist you in working with strings and structures, c-tree Plus includes a copy function, cpybuf(). c-tree Plus redefines cpybuf() to memcpy() in those environments which support this ANSI function. If not, cpybuf() is defined as:

void cpybuf(destination, source, length)
pTEXT   destination;
pTEXT   source;
UINT    length;

This function copies length bytes from source to destination. It copies strings, structures, or buffers. It will NOT stop when it sees a null character in source. cpybuf() stops when it has copied length bytes. We strongly suggest using this function when you know the length of the buffer or string you are copying.

c-tree Plus also includes cpybig(), which provides the same functionality as cpybuf(), but works for very large buffers even on machines with segmented memory models. The length parameter is declared as VRLEN instead of UINT, which provides for larger buffers.