目前共有6篇帖子。
memset的用法
1樓 巨大八爪鱼 2015-6-15 17:54
#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;
}
2樓 巨大八爪鱼 2015-6-15 17:58
memset是计算机中C/C++语言函数。将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASCII值
3樓 巨大八爪鱼 2015-6-15 18:12
#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
*/
4樓 巨大八爪鱼 2015-6-15 18:13
memset(ch + 3, '\0', 1);
At the 3 + 1 th char, repeat '\0' one time.
5樓 巨大八爪鱼 2015-6-15 18:18
6樓 巨大八爪鱼 2015-8-13 12:18
另外,如果把“This is a pen.“中的首字母T看作第0个字符的话(ch + 0),那么ch + 3就是第3个字符“s”,这个和数组是差不多的。数组下标也是从0开始的。

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
 
 
©2010-2025 Arslanbar [手機版] [桌面版]
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。