设置
|
登录
|
注册
首页
>
C语言吧
>
浏览帖子
回复帖子
|
只看楼主
目前共有
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
http://baike.baidu.com/picture/653964/653964/0/c2fdfc039245d6884024bf82a6c27d1ed21b24f5.html?fr=lemma&ct=single#aid=0&pic=c2fdfc039245d6884024bf82a6c27d1ed21b24f5
6楼
巨大八爪鱼
2015-8-13 12:18
另外,如果把「This is a pen.「中的首字母T看作第0個字元的話(ch + 0),那麼ch + 3就是第3個字元「s」,這個和數組是差不多的。數組下標也是從0開始的。
内容转换:
不转换
大陆简体
台灣正體
港澳繁體
马新简体
回复帖子
内容:
用户名:
您目前是匿名发表
验证码:
看不清?换一张
©2010-2025 Arslanbar [手机版] [
桌面版
]
除非另有声明,
本站
采用
知识共享署名-相同方式共享 3.0 Unported许可协议
进行许可。