C string
From Wikipedia, the free encyclopedia
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 C programs, strings are usually handled with string pointers, which hold the memory location of the first character of the string. The length of the string is not stored, and is instead calculated using strlen, which counts the the number of characters, starting at the pointer's memory location, before a null character is reached.
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. It may also cause a buffer overflow.
Contents |
[edit] C String header
The C standard library named string.h (<cstring> header) is used to work with C strings. Confusion or programming errors arise when strings are treated as simple data types. Specific functions have to be employed for comparison and assignment such as strcpy for assignment instead of the standard = and strncmp instead of == for comparison.
| Operation | Function | Description |
|---|---|---|
| Copying | ||
memcpy |
Copies a block of memory | |
memmove |
Move block of memory | |
strcpy |
Copy string | |
strncpy |
Copy n number characters from string | |
| Concatenation | ||
strcat |
Concatenate strings | |
strncat |
Append n number of characters from string | |
| Comparison | ||
memcmp |
Compare two blocks of memory | |
strcmp |
Compare two strings | |
strcoll |
Compare two strings using locale | |
strncmp |
Compare first n 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 |
[edit] Trivia
C strings are exactly equivalent to the strings created by the .ASCIZ directive implemented by the PDP-11 and VAX macroassembly languages.
[edit] See also
[edit] References
http://www.cplusplus.com/reference/clibrary/cstring/
|
|||||||||||||||||

