strncpy

2018-11-11 16:03 更新

 原型:extern char *strncpy(char *dest, char *src, int n);
       
 用法:#include <string.h>
 
 功能:把src所指由NULL結(jié)束的字符串的前n個(gè)字節(jié)復(fù)制到dest所指的數(shù)組中。
 
 說明:
       如果src的前n個(gè)字節(jié)不含NULL字符,則結(jié)果不會(huì)以NULL字符結(jié)束。
       如果src的長(zhǎng)度小于n個(gè)字節(jié),則以NULL填充dest直到復(fù)制完n個(gè)字節(jié)。
       src和dest所指內(nèi)存區(qū)域不可以重疊且dest必須有足夠的空間來容納src的字符串。
       返回指向dest的指針。
 
 舉例:


     // 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;
     }

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)