3

Almost every other program or function designed to copy one object into another uses the basic convention

copy(source, destination)

Even common logic would lead one to the conclusion that you are copying something FROM here to THERE. Why does strcpy use the exact opposite convention? It only serves to confuse people attempting to use the function. Surely the author of strcpy thought of this while he was writing the function? Or does the strcpy function predate this now-ubiquitous usage convention?

charliehorse55
  • 1,940
  • 5
  • 24
  • 38

1 Answers1

4

They wanted to match memcpy's syntax.

char * strcpy ( char * destination, const char * source );

void * memcpy ( void * destination, const void * source, size_t num );

Other C functions use this order too.

A basic strcpy(...) implementation would be (skipping castings):

char * strcpy ( char * destination, const char * source ) {
    return memcpy(destination, source, strlen(source) + 1);
}
MacAndor
  • 205
  • 2
  • 6
  • 2
    `memcpy(destination, source, 1+strlen(source));` – wildplasser Apr 20 '12 at 10:35
  • ... you wouldn't need casts anyway: `memcpy()` takes `void*` arguments (one of them `const` qualified), and `void*` is compatible with any other pointer type. – pmg Apr 20 '12 at 10:36
  • 1
    "They wanted to match memcpy's syntax." Which leads to the next question. Why is memcpy that way too? This answer basically says, it is this way because it is. – David Heffernan Apr 20 '12 at 10:42
  • 1
    To match assignment operator: dst = src. At least I think so. – blaze Apr 20 '12 at 10:45
  • @DavidHeffernan Totally agree, memcpy practically *is* strcpy. Not to mention this stuff has been around since at least 1978's first edition K&R, but according to this thread dst/src may have flipped??!!: https://stackoverflow.com/questions/25300040/is-it-possible-to-find-out-when-the-current-syntax-of-strcpy-was-added-to-the-c – ebyrob Jun 30 '17 at 14:41