If a function has nothing specific to return, it is often customary to return one of the input parameters (the one that is seen as the primary one). Doing this allows you to use "chained" function calls in expressions. For example, you can do

char buffer[1024];
strcat(strcpy(buffer, "Hello"), " World");

specifically because strcpy returns the original dst value as its result. Basically, when designing such a function, you might want to choose the most appropriate parameter for "chaining" and return it as the result (again, if you have noting else to return, i.e. if otherwise your function would return void).

Some people like it, some people don't. It is a matter of personal preference. C standard library often supports this technique, memcpy being another example. A possible use case might be something along the lines of

char *clone_buffer(const char *buffer, size_t size)
{
   return memcpy(new char[size], buffer, size);
}

If memcpy did not return the destination buffer pointer, we'd probably have to implement the above as

char *clone_buffer(const char *buffer, size_t size)
{
   char *clone = new char[size];
   memcpy(clone, buffer, size);
   return clone;
}

which looks "longer". There's no reason for any difference in efficiency between these two implementations. And it is arguable which version is more readable. Still many people might appreciate the "free" opportunity to write such concise one-liners as the first version above.

Quite often people find it confusing that memcpy returns the destination buffer pointer, because there is a popular belief that returning a pointer form a function should normally (or always) indicate that the function might allocate/reallocate memory. While this might indeed indicate the latter, there's no such hard rule and there has never been, so the often expressed opinion that returning a pointer (like memcpy does) is somehow "wrong" or "bad practice" is totally unfounded.

Answer from AnT stands with Russia on Stack Overflow
Top answer
1 of 5
66

If a function has nothing specific to return, it is often customary to return one of the input parameters (the one that is seen as the primary one). Doing this allows you to use "chained" function calls in expressions. For example, you can do

char buffer[1024];
strcat(strcpy(buffer, "Hello"), " World");

specifically because strcpy returns the original dst value as its result. Basically, when designing such a function, you might want to choose the most appropriate parameter for "chaining" and return it as the result (again, if you have noting else to return, i.e. if otherwise your function would return void).

Some people like it, some people don't. It is a matter of personal preference. C standard library often supports this technique, memcpy being another example. A possible use case might be something along the lines of

char *clone_buffer(const char *buffer, size_t size)
{
   return memcpy(new char[size], buffer, size);
}

If memcpy did not return the destination buffer pointer, we'd probably have to implement the above as

char *clone_buffer(const char *buffer, size_t size)
{
   char *clone = new char[size];
   memcpy(clone, buffer, size);
   return clone;
}

which looks "longer". There's no reason for any difference in efficiency between these two implementations. And it is arguable which version is more readable. Still many people might appreciate the "free" opportunity to write such concise one-liners as the first version above.

Quite often people find it confusing that memcpy returns the destination buffer pointer, because there is a popular belief that returning a pointer form a function should normally (or always) indicate that the function might allocate/reallocate memory. While this might indeed indicate the latter, there's no such hard rule and there has never been, so the often expressed opinion that returning a pointer (like memcpy does) is somehow "wrong" or "bad practice" is totally unfounded.

2 of 5
14

IIRC, in early versions of C there was no void return. So library functions which have been around long enough return something for legacy reasons, and this was the best they could come up with.

There are a bunch of functions in string.h which return the destination parameter: memcpy, strcpy, strcat. It's not very useful, but it does no harm (probably in many calling conventions doesn't even require an instruction to implement).

You might conceivably come up with a use: char *nextbuf = memcpy(get_next_buf(), previous_buf+offset, previous_size-offset); instead of char *nextbuf = get_next_buf(); memcpy(nextbuf, etc); Or something.

For comparison, qsort returns void. It could have been defined to return base on the principle of "return something, it might come in handy", but wasn't. std::copy rather more usefully returns an iterator to the end of the output range. For non-random-access iterators that might not be trivial, or even possible, for the caller to compute.

🌐
GeeksforGeeks
geeksforgeeks.org › c++ › memcpy-in-cc
memcpy() in C - GeeksforGeeks
September 22, 2025 - memcpy() copies the actual bytes of the memory you specify. If you pass it a pointer variable, it copies the pointer’s value (the address).
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_memcpy.htm
C library - memcpy() function
#include <stdio.h> #include <string.h> int main () { const char src[50] = "Tutorialspoint"; char dest[50]; strcpy(dest,"Heloooo!!"); printf("Before memcpy dest = %s\n", dest); memcpy(dest, src, strlen(src) + 1); printf("After memcpy dest = %s\n", dest); return(0); }
🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › memcpy.3.html
memcpy(3) - Linux manual page
The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap. The memcpy() function returns a pointer to dest.
🌐
MKSSoftware
mkssoftware.com › docs › man3 › memcpy.3.asp
memcpy() -- copy memory block
memcpy() copies n bytes from memory area s2 to s1. It returns s1.
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re173.html
memcpy - C in a Nutshell [Book]
December 16, 2005 - The return value is the same as the first argument, dest. The two pointer values must be at least n bytes apart, so that the source and destination blocks do not overlap; otherwise, the function’s behavior is undefined.
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
Scaler
scaler.com › home › topics › memcpy in c
memcpy() in C
April 14, 2024 - This helps to overcome the problem of overlapping addresses. The memcpy function in C returns a void pointer that points to the starting address of the destination memory location in which final values are stored.
Top answer
1 of 6
34

In early versions of the C language, every function would return something, whether or not the caller would make use of the returned value. Generally, the return value of a function would be whatever happened to be in some particular register of the appropriate type. If code exited a function without making any effort to set the register to something meaningful, and calling code ignored the contents of the register in question, having the function nominally return a meaningless value was simpler and easier than providing a means of having functions not return a value.

I don't think any particular thought was put into the question of what functions like memcpy, strcpy, or strcat should return, but the authors of the Standard didn't want to simply leave the return value unspecified. Since there may have been platforms where functions that don't return a value would be processed differently from those that do, giving such functions a void return type could have broken code that calls the functions without including the appropriate standard header.

I don't think any particular effort was made to have the functions return the most useful value. More likely, the authors of the Standard wanted to have the functions return some specified value, and so they somewhat arbitrarily picked a value to be returned.

2 of 6
21

So that you can write

s1 = memcpy (s2, memcpy (s3, memcpy (s4, s5)));

which is probably not particularily useful (for string concatenation using strcat, it is, however, and the memxxx and strxxx functions use aligned function signatures).

🌐
cppreference.com
en.cppreference.com › c › string › byte › memcpy
memcpy, memcpy_s - cppreference.com
As with all bounds-checked functions, memcpy_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <string.h>. ... 2) Returns zero on success and non-zero value on error.
Find elsewhere
🌐
Cplusplus
cplusplus.com › reference › cstring › memcpy
memcpy
Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination. The underlying type of the objects pointed to by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
🌐
Quora
quora.com › In-C-why-does-memcpy-return-its-first-argument
In C, why does memcpy return its first argument? - Quora
Answer (1 of 3): > In C, why does memcpy return its first argument? Historical inertia. Prior to the 1989 ANSI C standard, C did not have the [code ]void[/code] keyword. Every function had to return some value, if only implicitly. (If no return type is declared, the return type would be [code ]...
🌐
IBM
ibm.com › docs › en › i › 7.4.0
memcpy() — Copy Bytes
We cannot provide a description for this page right now
🌐
University of Helsinki
cs.helsinki.fi › group › boi2016 › doc › cppreference › reference › en.cppreference.com › w › c › string › byte › memcpy.html
memcpy, memcpy_s - cppreference.com
As all bounds-checked functions, memcpy_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including string.h. ... 2) Returns zero on success and non-zero value on error.
🌐
SAS
support.sas.com › documentation › onlinedoc › ccompiler › doc700 › html › lr1 › z2055843.htm
memcpy - Function Descriptions - SAS Support
For more information on optimizing your use of memcpy , see Optimizing Your Use of memcmp, memcpy, and memset. #include <string.h> #include <stdio.h> #define TAILSIZE 10 main() { char buf[160]; char tail[TAILSIZE+1]; puts("Enter a line of text."); gets(buf); if (strlen(buf) < TAILSIZE) printf("Your input was shorter than %d characters.\n", TAILSIZE); else{ memcpy(tail, buf+strlen(buf)-TAILSIZE, TAILSIZE+1); /* Copy last 10 characters of buf, */ /* plus the trailing null.
🌐
GitHub
github.com › coruus › safeclib › blob › master › src › safeclib › memcpy_s.c
safeclib/src/safeclib/memcpy_s.c at master · coruus/safeclib
* If there is a runtime-constraint violation, the memcpy_s function · * stores zeros in the first dmax bytes of the region pointed to · * by dest if dest is not a null pointer and smax is valid. * * RETURN VALUE · * EOK successful operation ·
Author   coruus
🌐
W3Resource
w3resource.com › c-programming › string › c-memcpy.php
C memcpy() function
2 weeks ago - #include <stdio.h> #include <string.h> int main() { // Define and initialize two integer arrays int source[5] = {1, 2, 3, 4, 5}; int destination[5]; // Copy contents of source array to destination array memcpy(destination, source, sizeof(source)); // Display the destination array contents printf("Copied array: "); for(int i = 0; i < 5; i++) { printf("%d ", destination[i]); } return 0; }
🌐
Lsu
ld2015.scusa.lsu.edu › cppreference › en › c › string › byte › memcpy.html
memcpy, memcpy_s - cppreference.com
As all bounds-checked functions, memcpy_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including string.h. ... 2) Returns zero on success and non-zero value on error.
🌐
SAS
support.sas.com › documentation › onlinedoc › sasc › doc700 › html › lr1 › z2055843.htm
Function Descriptions : memcpy
For more information on optimizing your use of memcpy , see Optimizing Your Use of memcmp, memcpy, and memset. #include <string.h> #include <stdio.h> #define TAILSIZE 10 main() { char buf[160]; char tail[TAILSIZE+1]; puts("Enter a line of text."); gets(buf); if (strlen(buf) < TAILSIZE) printf("Your input was shorter than %d characters.\n", TAILSIZE); else{ memcpy(tail, buf+strlen(buf)-TAILSIZE, TAILSIZE+1); /* Copy last 10 characters of buf, */ /* plus the trailing null.