Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
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. –  Kevin Nov 8 '11 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. –  Keith Thompson Nov 8 '11 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. –  Keith Thompson Nov 8 '11 at 21:13
1  
mov eax, 0xffffffffh; is your answer. –  Coder Nov 8 '11 at 21:32
1  
@Coder: movl 0xffffffff, %eax. I don't get your point. –  FUZxxl Nov 8 '11 at 21:47
show 3 more comments

3 Answers

up vote 5 down vote accepted

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
share|improve this answer
add comment

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.

share|improve this answer
    
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 –  Martin Beckett Jul 14 '12 at 14:08
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.