You can use the functions described in this section to copy the contents of strings, wide strings, and arrays. The ‘str’ and ‘mem’ functions are declared in string.h while the ‘w’ functions are declared in wchar.h. A helpful way to remember the ordering of the arguments to the functions in this section is that it corresponds to an assignment expression, with the destination array specified to the left of the source array. Most of these functions return the address of the destination array; a few return the address of the destination's terminating null, or of just past the destination.
Most of these functions do not work properly if the source and destination arrays overlap. For example, if the beginning of the destination array overlaps the end of the source array, the original contents of that part of the source array may get overwritten before it is copied. Even worse, in the case of the string functions, the null byte marking the end of the string may be lost, and the copy function might get stuck in a loop trashing all the memory allocated to your program.
All functions that have problems copying between overlapping arrays are
explicitly identified in this manual. In addition to functions in this
section, there are a few others like sprintf (see Formatted Output Functions) and scanf (see Formatted Input Functions).
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
The
memcpyfunction copies size bytes from the object beginning at from into the object beginning at to. The behavior of this function is undefined if the two arrays to and from overlap; usememmoveinstead if overlapping is possible.The value returned by
memcpyis the value of to.Here is an example of how you might use
memcpyto copy the contents of an array:struct foo *oldarray, *newarray; int arraysize; ... memcpy (new, old, arraysize * sizeof (struct foo));
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
The
wmemcpyfunction copies size wide characters from the object beginning at wfrom into the object beginning at wto. The behavior of this function is undefined if the two arrays wto and wfrom overlap; usewmemmoveinstead if overlapping is possible.The following is a possible implementation of
wmemcpybut there are more optimizations possible.wchar_t * wmemcpy (wchar_t *restrict wto, const wchar_t *restrict wfrom, size_t size) { return (wchar_t *) memcpy (wto, wfrom, size * sizeof (wchar_t)); }The value returned by
wmemcpyis the value of wto.This function was introduced in Amendment 1 to ISO C90.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
The
mempcpyfunction is nearly identical to thememcpyfunction. It copies size bytes from the object beginning atfrominto the object pointed to by to. But instead of returning the value of to it returns a pointer to the byte following the last written byte in the object beginning at to. I.e., the value is((void *) ((char *)to+size)).This function is useful in situations where a number of objects shall be copied to consecutive memory positions.
void * combine (void *o1, size_t s1, void *o2, size_t s2) { void *result = malloc (s1 + s2); if (result != NULL) mempcpy (mempcpy (result, o1, s1), o2, s2); return result; }This function is a GNU extension.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
The
wmempcpyfunction is nearly identical to thewmemcpyfunction. It copies size wide characters from the object beginning atwfrominto the object pointed to by wto. But instead of returning the value of wto it returns a pointer to the wide character following the last written wide character in the object beginning at wto. I.e., the value is wto+size.This function is useful in situations where a number of objects shall be copied to consecutive memory positions.
The following is a possible implementation of
wmemcpybut there are more optimizations possible.wchar_t * wmempcpy (wchar_t *restrict wto, const wchar_t *restrict wfrom, size_t size) { return (wchar_t *) mempcpy (wto, wfrom, size * sizeof (wchar_t)); }This function is a GNU extension.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
memmovecopies the size bytes at from into the size bytes at to, even if those two blocks of space overlap. In the case of overlap,memmoveis careful to copy the original values of the bytes in the block at from, including those bytes which also belong to the block at to.The value returned by
memmoveis the value of to.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
wmemmovecopies the size wide characters at wfrom into the size wide characters at wto, even if those two blocks of space overlap. In the case of overlap,wmemmoveis careful to copy the original values of the wide characters in the block at wfrom, including those wide characters which also belong to the block at wto.The following is a possible implementation of
wmemcpybut there are more optimizations possible.wchar_t * wmempcpy (wchar_t *restrict wto, const wchar_t *restrict wfrom, size_t size) { return (wchar_t *) mempcpy (wto, wfrom, size * sizeof (wchar_t)); }The value returned by
wmemmoveis the value of wto.This function is a GNU extension.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function copies no more than size bytes from from to to, stopping if a byte matching c is found. The return value is a pointer into to one byte past where c was copied, or a null pointer if no byte matching c appeared in the first size bytes of from.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function copies the value of c (converted to an
unsigned char) into each of the first size bytes of the object beginning at block. It returns the value of block.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function copies the value of wc into each of the first size wide characters of the object beginning at block. It returns the value of block.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This copies bytes from the string from (up to and including the terminating null byte) into the string to. Like
memcpy, this function has undefined results if the strings overlap. The return value is the value of to.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This copies wide characters from the wide string wfrom (up to and including the terminating null wide character) into the string wto. Like
wmemcpy, this function has undefined results if the strings overlap. The return value is the value of wto.
Preliminary: | MT-Safe | AS-Unsafe heap | AC-Unsafe mem | See POSIX Safety Concepts.
This function copies the string s into a newly allocated string. The string is allocated using
malloc; see Unconstrained Allocation. Ifmalloccannot allocate space for the new string,strdupreturns a null pointer. Otherwise it returns a pointer to the new string.
Preliminary: | MT-Safe | AS-Unsafe heap | AC-Unsafe mem | See POSIX Safety Concepts.
This function copies the wide string ws into a newly allocated string. The string is allocated using
malloc; see Unconstrained Allocation. Ifmalloccannot allocate space for the new string,wcsdupreturns a null pointer. Otherwise it returns a pointer to the new wide string.This function is a GNU extension.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function is like
strcpy, except that it returns a pointer to the end of the string to (that is, the address of the terminating null byteto + strlen (from)) rather than the beginning.For example, this program uses
stpcpyto concatenate ‘foo’ and ‘bar’ to produce ‘foobar’, which it then prints.#include <string.h> #include <stdio.h> int main (void) { char buffer[10]; char *to = buffer; to = stpcpy (to, "foo"); to = stpcpy (to, "bar"); puts (buffer); return 0; }This function is part of POSIX.1-2008 and later editions, but was available in the GNU C Library and other systems as an extension long before it was standardized.
Its behavior is undefined if the strings overlap. The function is declared in string.h.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function is like
wcscpy, except that it returns a pointer to the end of the string wto (that is, the address of the terminating null wide characterwto + wcslen (wfrom)) rather than the beginning.This function is not part of ISO or POSIX but was found useful while developing the GNU C Library itself.
The behavior of
wcpcpyis undefined if the strings overlap.
wcpcpyis a GNU extension and is declared in wchar.h.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This macro is similar to
strdupbut allocates the new string usingallocainstead ofmalloc(see Variable Size Automatic). This means of course the returned string has the same limitations as any block of memory allocated usingalloca.For obvious reasons
strdupais implemented only as a macro; you cannot get the address of this function. Despite this limitation it is a useful function. The following code shows a situation where usingmallocwould be a lot more expensive.#include <paths.h> #include <string.h> #include <stdio.h> const char path[] = _PATH_STDPATH; int main (void) { char *wr_path = strdupa (path); char *cp = strtok (wr_path, ":"); while (cp != NULL) { puts (cp); cp = strtok (NULL, ":"); } return 0; }Please note that calling
strtokusing path directly is invalid. It is also not allowed to callstrdupain the argument list ofstrtoksincestrdupausesalloca(see Variable Size Automatic) can interfere with the parameter passing.This function is only available if GNU CC is used.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This is a partially obsolete alternative for
memmove, derived from BSD. Note that it is not quite equivalent tomemmove, because the arguments are not in the same order and there is no return value.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This is a partially obsolete alternative for
memset, derived from BSD. Note that it is not as general asmemset, because the only value it can store is zero.