Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 2nd, 2006, 5:20 PM   #1
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
whitebook

from the k&r
void strcpy(char *s, char *t)
{
      while((*s++ = *t++) != '\0');
}

obviously this code copies one string to another, but my question is..."do C programmers really code like this?" reading the K&R has enhanced my thoughts of what can be done with a line of code, but they seem to praise code like ths when it can be done in a few more lines with a sense of clarity. (they do give examples of such code).

THE REAL DEAL...are they saying this is good coding practice, or are they coming from the 1960's+ UNIX environment where this counterintuitive crap was a necessity because of low memory capabilities?

either way, you gotta love the simplicity of placing so many operations on one line. i would highly reccomend this book to any coder.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Apr 2nd, 2006, 6:52 PM   #2
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
The big problem with C is obfuscation.

I've been writing C for a long time. Stuff like that is cool - but not a good idea.
Unfortunately because it's in K&R it has a sort of special appeal to C programmers.
jim mcnamara is offline   Reply With Quote
Old Apr 2nd, 2006, 7:10 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
coming from the 1960's+ UNIX environment where this counterintuitive crap was a necessity because of low memory capabilities?
Yup. Personally, I would have written it in even shorter form.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 2nd, 2006, 9:26 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
Isn't the != '\0' part unnecessary?
Jimbo is offline   Reply With Quote
Old Apr 2nd, 2006, 9:46 PM   #5
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
Quote:
Originally Posted by Jimbo
Isn't the != '\0' part unnecessary?
*t is being copied into *s until it finds '\0' in *t. If it weren't for the condition != '\0', it would continue copying into *s past from the string that t is pointing to, whatever that's in memory there. Plus, if there's no condition; how would the loop end? '\0' determines the end of a string.
Navid is offline   Reply With Quote
Old Apr 2nd, 2006, 10:41 PM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
its in C, where everything is implicitly cast to a boolean value, and any non-zero value is true
Jimbo is offline   Reply With Quote
Old Apr 3rd, 2006, 12:45 AM   #7
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
You're right, it is unnecessary, but I don't understand why myself. How will (*s++ = *t++) ever return 0?
Navid is offline   Reply With Quote
Old Apr 3rd, 2006, 1:28 AM   #8
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
An assignment operation always returns the new value of the object to assign, so someVariable = 3 would return 3 (return is not the good word here, I know, but I don't know anything better). When you assign *s++ = *t++, you assign *t to *s, t and s are increased, and it returns the new value of s, so if it is the null character, '\0', which is equal to false.
Polyphemus_ is offline   Reply With Quote
Old Apr 3rd, 2006, 3:13 AM   #9
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Jimbo
Isn't the != '\0' part unnecessary?
It is necessary, you don't make a compare with (*s++ = *t++), you assign. So that would always return true. Unless you assign it false, but you don't because characters are greater than 0. So would never stop and you would write beyond your string, which in turn will probably get you a segfault or something else bad.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 3rd, 2006, 3:44 AM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by nnxion
It is necessary, you don't make a compare with (*s++ = *t++), you assign. So that would always return true. Unless you assign it false, but you don't because characters are greater than 0. So would never stop and you would write beyond your string, which in turn will probably get you a segfault or something else bad.
Incorrect. An assignment is an expression that yields the value assigned.

If we do
    result = (*s++ = *t++);
it is exactly equivalent to;
   result = (*s = *t);
   ++s; ++t;   /* the post increments */
or (with some oversimplification to avoid doing multiple assignments in one line of code);
   *s = *t;
   result = *t;
   ++s; ++t;   /* the post increments */
All of this means that the code
void strcpy(char *s, char *t)
{
      while(*s++ = *t++);
}
achieves exactly the same thing as;
void strcpy(char *s, char *t)
{
   int result = 1;   /* start non-zero */
   while (result)
   {
         *s = *t;
         result = *t;
         ++s; ++t;  
   }
}

Note: the standard strcpy() returns it's first argument, not void.

As to the question of whether such code is good practice ..... it was sort-of desirable a couple of decades back due to limited memory and capacity of compilers to handle large bodies of code, but people tended to go overboard with it and make unreadable code. Now, readability is more important as programmer time is more expensive than extra memory for a computer.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC