Reprint please specify the source!
In the C language there is no specific string data type, string constants and character arrays in the form of strings.
The string is actually a string of 0 or more characters. And at the end of the entire bit mode 0NUL byte. Therefore, nul bytes cannot appear inside characters that are included in the string. The header file String.h includes the prototypes and declarations required to use the string function. Although not necessary. But it's really a good idea to include this header file in the program. Because of the prototypes it includes, the compiler is better able to run error checking for your program.
The following is a list of considerations for using the string function in library C, which will continue to be updated!
1. String lengthThe function prototypes for calculating string lengths in the C library are as follows:
size_t strlen (cha const *string);
Note that the return value of the function is an unsigned integer, size_t is defined in stddef.h, and it is assumed that using an unsigned integer in an expression can result in unpredictable results. For example, the following two expressions are equivalent:
if (strlen (x) >= strlen (y)) ... if (strlen (x)-strlen (y) >= 0) ...
In fact, they are not equal. There is no problem with the first expression, but the second expression is unsigned because the return value of strlen, so the expression on the left of >= is unsigned, so the expression is always true!
2. Unrestricted String functions
The string functions that we often use are "unrestricted", meaning that they infer the length of a string by looking for the NUL character at the end of the number of strings. These functions typically specify a block of memory to hold the resulting string, and the program ape will need to ensure that the resulting string cannot exceed the chunk memory area.
2.1. Copying strings
The function prototype for the function strcpy to copy the string is as follows:
char * strcpy (char *dst, const CHAR*SRC);
The function copies the strings in SRC to DST, assuming that there are overlapping portions in the two strings, the result is undefined.
The target's past content is overwritten and lost, even though SRC is shorter than the DST length.
Like what:
<pre name= "code" class= "CPP" >char str[] = "Hello, china!" strcpy (str, "Nihao");p rintf ("str =%s\n", str); output:nih Ao
The program ape must ensure that the target character array is space enough to accommodate the copied string. Assuming that the string is copied identically than the number of characters and the extra characters, they overwrite the data stored in the memory space after the array, strcpy cannot solve the problem, he cannot infer the length of the target character array!
2.2 Connection string
Strcat that a string is stitched to the back of a string, and its prototype is such as the following:
Char *strcat (char *dst, char const *SRC);
Strcat requires DST to include a string (capable of being an empty string), which searches to the end of the string and copies the string in src to that location. Assuming that SRC and DST overlap, the result is undefined.
Same. The program ape must ensure that the space of the DST character array can accommodate the total length of DST and SRC characters!
2.3 String comparison
The comparison of strings is a comparison of the two strings, until the discovery does not match. The function prototypes for strcmp are:
int strcmp (char const *S1, char cosnt *s2);
Suppose S1<s2, which returns a value less than 0. Assuming that S1 is greater than S2, it returns a value greater than 0. Assume equal to its return zero.
For the inference of whether the strings are equal, some people are writing such as the following:
The strcmp must end with a nul byte. Otherwise, it makes no sense to compare the bytes following the number of parameters.
3. Length-Constrained String functions
The C library includes other string processing functions that provide an explicit length to limit the length of the copied and compared strings, which effectively prevents unpredictable long strings from spilling over their targets.
, these functions cannot result in overlapping of source and target parameters.
The prototypes for these functions are as follows:
Char *strncap (char *dst, const char *SRC, size_t len), Char *stncat (char *dst, cosnt char *src, size_t len); int strncmp (Char cosnt *s1, Char const*s2, size_t len);
strncpy always precisely copies Len-length characters into DST, assuming that the value of strlen (SRC) is less than Len, the DST array is populated with extra nul bytes to Len length. Assuming that the value of strlen (SRC) is greater than or equal to Len, only Len bytes are copied into DST. Note: Its result will not end in NUL byte, this must be noticed!
This problem only occurs when you use the strncpy function to create a string. You can then use library functions that start with other Str or print them in printf using the%s format. Before you use an unrestricted function. We have to make sure that the string is actually a string with the NUL byte technique, consider the following example:
Char buffer[bsize];strncpy (buffer, name, bsize); buffer[bsize-1] = 0;
Assume that the name length is less than bsize. Then the final assignment statement has no effect, but assuming that name is too long to exceed bsize, then the final assignment statement will be able to control the end of buffer with nul bytes, and subsequent operations involving the string will work correctly.
Strncat always joins a nul byte at the end of the target string. Len does not speak the original character length, including, it just tube src in len copy the character Src in, and SRC joins the end nul byte. No matter the SRC string out the remaining space is pre-existing enough.
strncmp compares Len bytes, assuming that the inequality property before the two string Len character, the function returns, assuming that the first two string Len characters are equal, the function returns zero.
C Note that the language string used