:: Re: [DNG] Icerbergs aren't small ju…
Startseite
Nachricht löschen
Nachricht beantworten
Autor: Didier Kryn
Datum:  
To: dng
Betreff: Re: [DNG] Icerbergs aren't small just because they're mostly underwater
Le 25/01/2016 16:08, Rainer Weikusat a écrit :
> Didier Kryn <kryn@???> writes:
>> Le 25/01/2016 13:23, Rainer Weikusat a écrit :
>>>       while (*r) if (*r++ == '/') n = r;
>>      Does it mean

>>
>>      while (*r)
>>        {
>>          if (*r == '/')
>>     {
>>             n = r;
>>             r++;
>>          }
>>        }

>>
>> or
>>
>>      while (*r)
>>        {
>>          if (*r == '/')
>>     {
>>             r++;
>>             n = r;
>>          }
>>        }

>>
>>
>>      I think the second answer is the good one. It is more readable and
>> less error-prone than your example and
> ... doesn't work. r (for 'running pointer') needs to be incremented on
> every iteration until it hits the end of the string. In case it
> currently pointed to a '/', 'n' ('pointer to [start of] name') needs to
> be set to the char behind the slash. As soons as *r == 0 aka !*r, n will
> point to the char after the last slash in the original string, ie, to
> the program name part of a program pathname.

>
> This is even already 'optimized for simplicity' as gcc will (usually)
> issue code to reload the char r points and thus, if this was supposed
> 'optimized', it really ought to be something like (all untested)
>
> char const *r, *n;
> int c;
>
> n = r = arg0;
> while (c = *r++) if (c == '/') n = r;
>
> A multi-line version could look like this:
>
> while (c = *r) {
>     ++r;
>          if (c == '/') n = r;
> }

>


     It might be done with a for loop.  eg:


     for ( ; *r ; ++r) if(*r=='/') n=r;
n++;


The for loop is the best construct for a loop with an incremental
cursor. While is rather meant for things like

     while ( (c=fgets(s, sizeof(s), stdin) )


     At the end of the day, there are many ways to write even simple 
things :-)


     Didier