关键词搜索

源码搜索 ×
×

通过两个小程序看memset和memcpy的用法

发布2012-11-02浏览8490次

详情内容

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char str1[] = "abcdefg";
  6. memset(str1,'x', 3);
  7. cout << str1 << endl;
  8. int i, a[4];
  9. for(i = 0; i < 4; i++)
  10. cout << a[i] << "\t";
  11. cout << endl;
  12. memset(a, 0, sizeof(a) - 4);
  13. for(i = 0; i < 4; i++)
  14. cout << a[i] << "\t";
  15. cout << endl;
  16. memset(a, 0, sizeof(a));
  17. for(i = 0; i < 4; i++)
  18. cout << a[i] << "\t";
  19. cout << endl;
  20. return 0;
  21. }

       结果为:

xxxdefg
-858993460      -858993460      -858993460      -858993460
0       0       0       -858993460
0       0       0       0

 

 

 

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char dst[100] = "hello world!";
  6. char src[10] = "C++";
  7. memcpy(dst, src, strlen(src));
  8. cout << dst << endl;
  9. memcpy(dst, src, strlen(src) + 1);
  10. cout << dst << endl;
  11. memcpy(dst + strlen(dst), src, strlen(src) + 1);
  12. cout << dst << endl;
  13. return 0;
  14. }

         结果为:

C++lo world!
C++
C++C++

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载