One test string and one copy of memory content 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; } ------------------------------ 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: Copies 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; } ----------------------------------------------- Strcpy only copies strings, but does not limit the number of copies. This can easily cause buffer overflow. Strncpy should be safer. Memcpy can also be used to copy data in the memory. Because the string ends with zero, memcpy can only be used for data that contains zero. They do not necessarily differ in performance.
|