5

Answering a question about order of parameters it struck me that strcpy (and family) are the wrong way round. Copy should be src -> destination.

Is there a historical or architectural reason for the dest,src order in these 'C' functions? Something to do with optimization of the stack on the PDP-8 or something?

asked Nov 8, 2011 at 20:58
8
  • 1
    Why do you think it should be src->dst? You set regular variables dst = src, it's just doing this for multiple vars/memory locations at a time. Commented Nov 8, 2011 at 21:01
  • @littleadv: Not necessarily a duplicate; this is about src vs. dest, not buffer vs. size. It's largely arbitrary in both cases, but if there's a rationale it's likely to be different. Commented Nov 8, 2011 at 21:09
  • 2
    Whatever the rationale, the fact that the order is the same as for the operands of an assignment operator is a good way to remember it. In any case, the C standard library evolved. It's not a model of consistency; don't expect it to be. Commented Nov 8, 2011 at 21:13
  • 1
    mov eax, 0xffffffffh; is your answer. Commented Nov 8, 2011 at 21:32
  • 1
    @Coder: movl 0xffffffff, %eax. I don't get your point. Commented Nov 8, 2011 at 21:47

3 Answers 3

8

Think of it like an assignment operation.

A = B; //copies the contents of B into A

Same order when using memcpy to copy an array.

memcpy(A, B, sizeof(B)); //copies the contents of B into A
answered Nov 9, 2011 at 2:25
2

Putting the target as the second argument would be inconsistent with other functions that write things to strings and have the target in the first argument. memset(3) and sprintf(3) come to mind.

answered Nov 9, 2011 at 3:28
0

I don't think anyone will know for certain the true answer, although many will speculate why, and now we can no longer ask Dennis Ritchie, the definitive answer is probably not possible. (Can anyone provide an evidence based answer?)

However, I do belive you assertion that the order is "wrong" is wrong. It is what it is. On the right in the UK: there is no right or wrong, it's just because it's that way - as long as everyone does it the same way around.

Pubby
3,3901 gold badge23 silver badges28 bronze badges
answered Nov 8, 2011 at 23:43
1
  • There are logical reasons for driving on the left, and historical/political ones for switching to the right, but it's a bit OT for programmers.so Commented Jul 14, 2012 at 14:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.