設置
|
登錄
|
註冊
首頁
>
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 [手機版] [
桌面版
]
除非另有聲明,
本站
採用
創用CC姓名標示-相同方式分享 3.0 Unported許可協議
進行許可。