Differences between several common copy functions: strncpy, stpcpy, memcpy, strcpy, and memccpy

Source: Internet
Author: User

From: http://blog.sina.com.cn/s/blog_484237d5010002km.html

Strncpy

 
Prototype: extern char * strncpy (char * DEST, char * SRC, int N); usage: # include <string. h> function: copy the first n Bytes of the string ending with null in SRC to the array indicated by DeST. Note: If the first n Bytes of SRC do not contain null characters, the result will not end with null characters. If the SRC length is less than n Bytes, fill the Dest with null until the n Bytes are completely copied. The memory areas specified by Src and DEST cannot overlap and DEST must have enough space to hold SRC strings. Returns the pointer to DeST. Example: // 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 ;}
 
        
Stpcpy
 
Prototype: extern char * stpcpy (char * DEST, char * SRC); usage: # include <string. h> function: copy the string ending with null indicated by Src to the array indicated by DeST. Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings. Returns the pointer pointing to the end of the Dest character (null. Example: // stpcpy. C # include <syslib. h> # include <string. h> main () {char * s = "golden Global View"; char d [20]; clrscr (); stpcpy (D, S); printf ("% s ", d); getchar (); Return 0 ;}
Memcpy
 
Prototype: extern void * memcpy (void * DEST, void * SRC, unsigned int count); usage: # include <string. h> function: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST. Note: the memory areas specified by Src and DEST cannot overlap. The function returns a pointer to DeST. Example: // 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 ;}
Strcpy
 
Prototype: extern char * strcpy (char * DEST, char * SRC); usage: # include <string. h> function: copy the string ending with null indicated by Src to the array indicated by DeST. Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings. Returns the pointer to DeST. Example: // strcpy. C # include <syslib. h> # include <string. h> main () {char * s = "golden Global View"; char d [20]; clrscr (); strcpy (D, S); printf ("% s ", d); getchar (); Return 0 ;}
Memccpy
 
Prototype: extern void * memccpy (void * DEST, void * SRC, unsigned char CH, unsigned int count); usage: # include <string. h> function: the data is copied from the memory area indicated by Src to the memory area indicated by DeST. If the CH character is encountered, the data is stopped. Note: The pointer pointing to the first character after the CH character is returned. If ch does not exist in the First n Bytes of SRC, null is returned. Ch is copied. Example: // memccpy. C # include <syslib. h> # include <string. h> main () {char * s = "golden Global View"; char d [20], * P; clrscr (); P = memccpy (D, S, 'X', strlen (s); If (p) {* P = '\ 0'; // must do this printf ("char found: % S. \ n ", d);} else printf (" char not found. \ n "); getchar (); Return 0 ;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.