zoukankan      html  css  js  c++  java
  • memcpy() -- 拷贝内存函数

    定义函数: void *memcpy(void *dest, const void *src, size_t n)
    函数说明: memcpy()用来拷贝src所指的内存内容前n个字节到dest所指的内存地址上。与strcpy()不同的是,memcpy()会完整的复制n个字节,不会因为遇到字符串结束''而结束
    返回值:   返回指向dest的指针
    附加说明:指针src和dest所指的内存区域不可重叠
    --------------------------------------------------------------------------------------------------------------------------------------------

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    main()
    {
          char *s="Golden Global View";
          char d[20];
          memcpy(d,s,strlen(s));
          d[strlen(s)]=0;
          printf("%s ",d);
          system("pause");
    }

    ------------------------------------------------------------------------------------------------------------------------------------------------

    strcpy 与 memcpy

    ------------------------------------------------------------------------------------------------------------------------------------------------

    main()
    {
        char a[30] = "string (a)";
        char b[30] = "hizengxiaolong";
        int i;

        strcpy(a, b);             //a[30] = "hiing (a)"
        printf("strcpy():");
        for(i = 0; i < 30; i++)
        printf("%c", a[i]);   //hi ing (a)
        system("pause");

        memcpy(a, b, 30);         //a[30] = "hizengxiaolong"
        printf(" memcpy():");
        for(i = 0; i < 30; i++)
        printf("%c", a[i]);   //hi zengxiaolong
        printf(" i = %d ", i); //30
        system("pause");
    }

    ---------------------------------------------------------------------------------------------------------------------------------------------

  • 相关阅读:
    LeetCode--Reorder List
    LeetCode--Combination Sum
    LeetCode--Binary Tree Level Order Traversal
    LeetCode--Plus One
    第五届蓝桥杯决赛CC++B组——生物芯片
    第五届蓝桥杯决赛CC++B组——Log大侠
    第五届蓝桥杯决赛CC++B组——出栈次序
    1098 均分纸牌 ——http://codevs.cn/problem/1098/
    1294 全排列——http://codevs.cn/problem/1294/
    1501 二叉树最大宽度和高度——http://codevs.cn/problem/1501/
  • 原文地址:https://www.cnblogs.com/sagerking/p/6368054.html
Copyright © 2011-2022 走看看