Strncpy and memcpy Functions

Source: Internet
Author: User
Function Name: strncpy
Function: Copy strings
Usage: char * strncpy (char * Destin, char * Source, int maxlen );
Memcpy
Function: Copy n Bytes from the source to the target Destin.

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.

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.