【轉】C語言mem.h中的函數介紹

來源:互聯網
上載者:User

標籤:

函數名稱:     memccpy
函數原型:     void *memccpy(void *dest, const void *src, int c, size_t n)
函數功能:     字串拷貝,到指定長度或遇到指定字元時停止拷貝
函數返回:
參數說明:     src-源字串指標,c-中止拷貝檢查字元,n-長度,dest-拷貝底目的字串指標
所屬檔案:     string.h,mem.h

#include string.h
#include stdio.h
int main()
{
    char *src="This is the source string";
    char dest[50];
    char *ptr;
    ptr=memccpy(dest,src,‘c‘,strlen(src));
    if (ptr)
    {
    *ptr=‘/0‘;
        printf("The character was found:%s",dest);
    }
    else
    printf("The character wasn‘t found");
    return 0;
}


@函數名稱:     memchr
函數原型:     void *memchr(const void *s, int c, size_t n)
函數功能:     在字串s中前n個字元中尋找某個字元c的位置
函數返回:     返回c的位置指標,返回NULL時表示未找到
參數說明:     s-要搜尋的字串,c-要尋找的字元,n-指定長度
所屬檔案:     string.h,mem.h

#include string.h
#include stdio.h
int main()
{
    char str[17];
    char *ptr;
    strcpy(str,"This is a string");
    ptr=memchr(str,‘r‘,strlen(str));
    if(ptr)
    printf("The character ‘r‘ is at position:%d",ptr-str);
    else
    printf("The character was not found");
    return 0;
}

@函數名稱:     memcmp
函數原型:     int memcmp(const void *s1, const void *s2, size_t n)
函數功能:     按字典順序對字串s1,s2比較,並只比較前n個字元
函數返回:     返回數值表示比較結果
參數說明:     s1,s2-要比較的字串,n-比較的長度
所屬檔案:     string.h,mem.h

#include stdio.h
#include string.h

int main()
  {
    auto char buffer[80];

    strcpy(buffer,"world");
    if( memcmp(buffer,"would ",6)<0){
      printf("Less than/n");
    }
    return 0;
  }


@函數名稱:     memicmp
函數原型:     int memicmp(const void *s1, const void *s2, size_t n)
函數功能:     按字典順序、不考慮字母大小寫對字串s1,s2比較,並只比較前n個字元
函數返回:     返回數值表示比較結果
參數說明:     s1,s2-要比較的字串,n-比較的長度
所屬檔案:     string.h,mem.h

#include stdio.h
#include string.h
int main()
{
    char *buf1 = "ABCDE123";
    char *buf2 = "abcde456";
    int stat;
    stat = memicmp(buf1, buf2, 5);
    printf("The strings to position 5 are ");
    if(stat)
        printf("not");
    printf("the same");
    return 0;
}

@函數名稱:     memcpy
函數原型:     void *memcpy(void *dest, const void *src, size_t n)
函數功能:     字串拷貝
函數返回:     指向dest的指標
參數說明:     src-源字串,n-拷貝的最大長度
所屬檔案:     string.h,mem.h

#include stdio.h
#include string.h
int main()
{
    char src[] = "******************************";
    char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
    char *ptr;
    printf("destination before memcpy: %s",dest);
    ptr=memcpy(dest,src,strlen(src));
    if(ptr)
        printf("destination after memcpy:%s",dest);
    else
        printf("memcpy failed");
    return 0;
}

@函數名稱:     memmove
函數原型:     void *memmove(void *dest, const void *src, size_t n)
函數功能:     字串拷貝
函數返回:     指向dest的指標
參數說明:     src-源字串,n-拷貝的最大長度
所屬檔案:     string.h,mem.h

#include string.h
#include stdio.h
int main()
{
    char dest[40]="abcdefghijklmnopqrstuvwxyz0123456789";
    printf("destination prior to memmove:%s/n",dest);
    memmove(dest+1,dest,35);
    printf("destination after memmove:%s",dest);
    return 0;
}


@函數名稱:     memset
函數原型:     void *memset(void *s, int c, size_t n)
函數功能:     字串中的n個位元組內容設定為c
函數返回:
參數說明:     s-要設定的字串,c-設定的內容,n-長度
所屬檔案:     string.h,mem.h
#include string.h
#include stdio.h
#include <mem.h>
int main()
{
    char buffer[] = "Hello world";
    printf("Buffer before memset:%s",buffer);
    memset(buffer,‘*‘,strlen(buffer)-1);
    printf("Buffer after memset:%s",buffer);
    return 0;
}

【轉】C語言mem.h中的函數介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.