zoukankan      html  css  js  c++  java
  • C 函数 strcpy and memcpy, strdup, memcmp,memset 库函数皆小写

     void *memcpy(void *dst, const void *src, size_t n);

    //If copying takes place between objects that overlap, the behavior is undefined

    因此标准库提供了地址重叠时的内存拷贝函数:memmove(),

    该函数把源字符串拷贝到临时buf里,然后再从临时buf里写到目的地址

     

    strcpy   and  memcpy 

    memcpy 可以进行指定字节数量的任意类型数据的拷贝, 可以用于内存拷贝。

      char a[100], b[50];

      memcpy(b, a,sizeof(b)); //not sizeof(a),which may cause overflow

    strcpy只能用于拷贝字符串,遇到''终止拷贝。

      char a[100], b[50];

      strcpy(a,b);

    void *memset(void *buffer,int c,int count);

    把buffer所指内存区域的前count个字节设置成字符c, 一般用于对指定的字符串清零。

    below from    http://www.cnblogs.com/aprilapril/p/4333173.html

    ..........strdup("adm"); //as it shows itself, only valid to string type.

    memcpy function,return an adress pointed at heap which store"adm"(with string end sign).

    .........strcpy(pAim, "4021");
    "4021" is copied into the memory started at address pAim.

    ...........memcpy(void*to,void*from,int size)
    copy designated size  to the memory.

    ..........memcmp(void *a, void *b, int size)

    copare two storage, if equal,return to 0,otherwise, get a positive or negative value.

    estrdup

    #include <config.h>
    #include <stdlib.h>
    #include <err.h>
    
    #include "roken.h"
    
    /*
     * Like strdup but never fails.
     */
    
    ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL
    estrdup (const char *str)
    {
        char *tmp = strdup (str);
    
        if (tmp == NULL)
        errx (1, "strdup failed");
        return tmp;
    }
  • 相关阅读:
    第一个Django demo
    内建函数
    Git积累
    区间dp
    【Spring】Spring AOP详解(转载)
    【git】git 常用命令(含删除文件)
    【idea】idea如何在maven工程中引入jar包
    【Java】Scanner类nextInt后使用nextLine无法读取输入
    【Java_Eclipse】Eclipse插件如何卸载?
    【MySQL】MySQL5.7等以上版本在Windows上的配置
  • 原文地址:https://www.cnblogs.com/aprilapril/p/3191136.html
Copyright © 2011-2022 走看看