String functions, C string functions
1. memset
# Include <stdio. h> # include <string. h> int main () {// char * s = "My Dream Come true"; // The String constant used to initialize the character pointer will be scheduled by the compiler to the read-only data storage area, is an unchangeable char s [] = "My Dream Come true"; // allocate space for memset (s, 'B', 6) in the stack ); // printf ("% s \ n", s) in bytes; // bbbbbbam Come true return 0 ;}
Memset can easily clear a variable or array of the structure type.
Example:
Variable: struct position pos;
Memset (& pos, 0, sizeof (struct position ));
Array:
Struct position pos [10];
Memset (pos, 0, sizeof (struct position) * 10 );
2. memcpy
Strcpy is usually used to copy strings, while memcpy is generally used to copy data of other types.
Memcpy can copy any content, such as character arrays, integers, structs, and classes.
// Memcpy is used for memory copying. You can use it to copy any data type object. You can specify the copied Data Length # include <stdio. h> # include <string. h> int main () {char a [27] = {'\ 0'}, B [30] = {' \ 0'}; for (int I = 0; I <26; I ++) a [I] = 'A' + I; memcpy (B, A, sizeof (B )); // copy the content of array a to array B. The length of array B should not be less than a puts (B); // ABCD ..... Z return 0 ;}
If the length of the above array B is less than a, B will first be assigned a long string such as B array, In addition, will append the string of the entire array