TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Pascal Schmidt
from: Darin McBride
date: 2004-04-15 16:32:26
subject: Squares

Hello Pascal!

Replying to a message of Pascal Schmidt to Darin McBride:

 JB>>>  for (n=0; target[n] != '\0' ; n++) {
 DM>> I prefer this one.  ;-)
 PS> I would write this one as (minor difference only): 

 PS>     for (n = 0; target[n] != 0; n++) {
 PS>         ...
 PS>     }

 JB>>> and possibly even better re-written to use a char* instead
 JB>>>  for( s=target ; *s ; s++ ) {
 DM>> Yes, I like this even more ;-)

Better, IMO:

char* s;
for (s = target; *s != '\0'; ++s) { /* ... */ }

Here's what I like about it:

1. Uses pointer rather than index (I'll get into why later)
2. Compares char with char (compare to *s != 0 which compares char to integer)
3. Pre-increment is "faster" than post-increment.

For #2 - this is just a matter of preference.  While both compile to the
same code, I'm guessing, I make things very obvious that I am purposefully
comparing the same thing.  Similarly, when comparing unsigned longs to 0, I
usually do similarly: ul != 0UL.

For #3 - this is not technically true in this case.  However, it's a habit
I've gotten into in C++ where the loop variable might be a complex object
where there is more significant speed differences.  A pre-increment can
modify itself, while a post-increment must copy itself and then modify
itself, then return the copy (possibly requiring another copy!).  For large
objects being modified in a tight (long) loop, we can talk about
significant copying here.  There is no difference in this case with a
built-in variable type, so, like I said, it's just habit ;-)

 PS> I think that's obfuscated enough to require several seconds of thought
 PS> when looking at it some time after writing it. ;)

Nah - I like this one.  I suppose it's a forest/trees thing - with an
index, you concentrate on the array (forest), and with a pointer, you
concentrate on the single object that you're dealing with on this iteration
through the loop (tree).

It's also easier to write.  Compare my_string[my_index] to *my_ptr.

It also can be safer.  If you are not updating the string in the loop, you can do:

char const* p
for (p = target; '\0' != *p; ++p)
{
  /* modifications here to *p will be caught
   * by the compiler!
   */
}

This is a bit more painful to do with indexes - or, at least a bit less obvious.
char const* p = target;
for (index = 0; index < strlen_of_target; ++index)
{
   /* use p[index], not target[index]... */
}

 PS> However, I never needed to traverse a string this way in all the C
 PS> programs I have written so far.

You're right - you don't *need* to do things this way.  I still think
they're better ;->

Darin

---
* Origin: Tanktalus' Tower BBS (1:250/102)
SEEN-BY: 633/267 270
@PATH: 250/102 99 10/345 106/1 2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.