Null-terminated string

This is an old revision of this page, as edited by Ktracy (talk | contribs) at 01:08, 31 October 2007. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computing, a C string is a character sequence stored as a one-dimensional character array and terminated with a null character ('\0', called NUL in ASCII). The name refers to the ubiquitous C programming language which uses this string representation.

In the C++ programming language, C strings are used in addition to another representation of character sequences, the std::string container found in the Standard Template Library (STL). Thus, it is important to differentiate between the traditional "C strings" and the more sophisticated "string" objects provided by the STL.

The null-termination characteristic has historically created security problems related to the length of the string. If the null character is not correctly accounted for, any following non-related memory area may also be processed as a part of the character sequence. This can lead to program crashes or leakage of program internal information to attackers or non-understanding users.

C String header The <cstring> header and its functions are the ideal way to work with C strings. Typical operator use with C strings will frequently create problems with pointers. As an example, using the = operator between two C strings does not copy the array but copies a pointer to this array. This is incorrect usage for most typical applications.


Example:

To copy a C string correctly, the following format could be used memcpy(CstringDst, CstringSrc, strlen(CstringSrc) + 1);

Where CstringDst is the destination C string to be copied into and CstringSrc is the source of the C string. The last argument will set the length of the destination C array. It will set this length to the length of the source plus 1 to account for the terminating character.


The functions included in <cstring> are as follows:

Copying: memcpy Copies block of memory memmove Move block of memory strcpy Copy string strncpy Copy characters from string

Concatenation: strcat Concatenate strings strncat Append characters from string


Comparison: memcmp Compare two blocks of memory strcmp Compare two strings strcoll Compare two strings using locale strncmp Compare characters of two strings strxfrm Transform string using locale


Searching: memchr Locate character in block of memory strchr Locate first occurrence of character in string strcspn Get span until character in string strpbrk Locate character in string strrchr Locate last occurrence of character in string strspn Get span of character set in string strstr Locate substring strtok Split string into tokens


Other: memset Fill block of memory strerror Get pointer to error message string strlen Get string length


Trivia

C strings are exactly equivalent to the strings created by the .ASCIZ directive implemented by the PDP-11 and VAX macroassembly languages.

See also