 |
#include <stdio.h> #include <string.h>
int main() { char ch[] = "This is a pen."; memset(ch + 3, '*', 8); // begins at the 4th char printf("%s\n", ch); // Result: Thi********en. return 0; }
|
 |
memset是计算机中C/C++语言函数。将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASCII值
|
 |
#include <stdio.h> #include <string.h> // for memset #include <stdlib.h> // for calloc
int main() { char* ch = calloc(15, sizeof(char)); memset(ch, 'A', 14); printf("%s\n", ch); memset(ch + 3, '\0', 1); printf("%s\n", ch); memset(ch + 3, 'B', 1); printf("%s\n", ch); return 0; } /* AAAAAAAAAAAAAA AAA AAABAAAAAAAAAA */
|
 |
memset(ch + 3, '\0', 1); At the 3 + 1 th char, repeat '\0' one time.
|
 |
另外,如果把“This is a pen.“中的首字母T看作第0个字符的话(ch + 0),那么ch + 3就是第3个字符“s”,这个和数组是差不多的。数组下标也是从0开始的。
|