C語言中函數strcpy ,strncpy ,strlcpy的用法

C語言中函數strcpy ,strncpy ,strlcpy的用法 [日期:2012-06-03] 來源:Linux社區 做者:tigerjb [字體:大 中 小]數組

1>strncpy函數

原型:extern char *strncpy(char *dest, char *src, int n);字體

用法:#include <string.h>指針

功能:把src所指由NULL結束的字符串的前n個字節複製到dest所指的數組中。內存

說明:字符串

若是src的前n個字節不含NULL字符,則結果不會以NULL字符結束。get

若是src的長度小於n個字節,則以NULL填充dest直到複製完n個字節。src和dest所指內存區域不能夠重疊且dest必須有足夠的空間來容納src的字符串。原型

返回指向dest的指針。string

舉例:社區

// strncpy.c

#include <syslib.h>

#include <string.h>

main()

{

char *s="Golden Global View";

char *d="Hello, GGV Programmers";

char *p=strdup(s);

clrscr();

textmode(0x00); // enable 6 lines mode

strncpy(d,s,strlen(s));

printf("%s/n",d);

strncpy(p,s,strlen(d));

printf("%s",p);

getchar();

return 0;

}

2>memcpy

原型:extern void *memcpy(void *dest, void *src, unsigned int count);

用法:#include <string.h>

功能:由src所指內存區域複製count個字節到dest所指內存區域。

說明:src和dest所指內存區域不能重疊,函數返回指向dest的指針。

舉例:

// memcpy.c

#include <syslib.h>

#include <string.h>

main()

{

char *s="Golden Global View";

char d[20];

clrscr();

memcpy(d,s,strlen(s));

d[strlen(s)]=0;

printf("%s",d);

getchar();

return 0;

}

相關文章
相關標籤/搜索