作者共发了6篇帖子。 内容转换:不转换▼
 
点击 回复
307 5
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开始的。

回复帖子

内容:
用户名: 您目前是匿名发表
验证码:
(快捷键:Ctrl+Enter)
 

本帖信息

点击数:307 回复数:5
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2015-8-13 12:18
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。