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;
    }
  • 相关阅读:
    Go语言基础之字符串遍历
    Go语言基础之range
    Go语言的for循环
    Go语言基础之反射
    Go语言基础之接口
    Linux编程简介
    如何使用gcc编译器
    ADS的使用
    bvp4c--语法
    어느 도시 보유 하 면 사랑 이다(事態が発生すれば、ある都市の恋はしません)【Si les villes un amour】{If have love in a city}
  • 原文地址:https://www.cnblogs.com/aprilapril/p/3191136.html
Copyright © 2011-2022 走看看