![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Isn't the != '\0' part unnecessary?
|
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
|
Quote:
|
|
|
|
|
|
|
#6 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
its in C, where everything is implicitly cast to a boolean value, and any non-zero value is true
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
You're right, it is unnecessary, but I don't understand why myself. How will (*s++ = *t++) ever return 0?
|
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#10 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
If we do result = (*s++ = *t++); result = (*s = *t); ++s; ++t; /* the post increments */ *s = *t; result = *t; ++s; ++t; /* the post increments */ void strcpy(char *s, char *t)
{
while(*s++ = *t++);
}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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|