Code:
char *strcpy(char*dest, char*orig)
{
   char *tmp = dest;
   
   while (*dest++ = *orig++)
      ;
   return tmp;
}


My question is, why is it "return tmp" and not "return *tmp"?
The return type of that function is a pointer-to-character (char*). dest, orig, and tmp are all pointers to specific memory locations that contain characters (actually, sequences of characters that make up strings, but they could also be single characters and the function would look the same). tmp is already a char*, a pointer to the memory address of the first byte of the copied string. That's the value you want to return. If you returned *tmp, you'd be dereferencing the pointer and instead returning the value of the first character of the copied string.
Okay, thx Kerm
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement