Go to the first, previous, next, last section, table of contents.


String and Array Conventions

This chapter describes both functions that work on arbitrary arrays or blocks of memory, and functions that are specific to null-terminated arrays of characters.

Functions that operate on arbitrary blocks of memory have names beginning with `mem' (such as memcpy) and invariably take an argument which specifies the size (in bytes) of the block of memory to operate on. The array arguments and return values for these functions have type void *, and as a matter of style, the elements of these arrays are referred to as "bytes". You can pass any kind of pointer to these functions, and the sizeof operator is useful in computing the value for the size argument.

In contrast, functions that operate specifically on strings have names beginning with `str' (such as strcpy) and look for a null character to terminate the string instead of requiring an explicit size argument to be passed. (Some of these functions accept a specified maximum length, but they also check for premature termination with a null character.) The array arguments and return values for these functions have type char *, and the array elements are referred to as "characters".

In many cases, there are both `mem' and `str' versions of a function. The one that is more appropriate to use depends on the exact situation. When your program is manipulating arbitrary arrays or blocks of storage, then you should always use the `mem' functions. On the other hand, when you are manipulating null-terminated strings it is usually more convenient to use the `str' functions, unless you already know the length of the string in advance.


Go to the first, previous, next, last section, table of contents.