Re: A89: Clearing a string


[Prev][Next][Index][Thread]

Re: A89: Clearing a string




I have two gripes about some of this code. First, I personally believe you
should not assign and int into a char, so instead of str[i] = 0 you should
use str[i] = '\0', It may be equivalent, but the latter makes it more clear
that you are dealing with chars and not ints.

Second, I prefer to write empty for statements this way:

for (conditions)
    continue;

This makes it obvious that the body of the loop is not intended to do
anything. I'm picky about these things because I spend a lot of time
deciphering other people's code.

-Kevin

-----Original Message-----
From: ComAsYuAre@aol.com <ComAsYuAre@aol.com>
To: assembly-89@lists.ticalc.org <assembly-89@lists.ticalc.org>
Date: Monday, July 03, 2000 2:50 PM
Subject: Re: A89: Clearing a string


>
>Hehe I wrote that :-)  This was Olle's code:
>
>int i; for(i=0;str[i++]!=0;str[i]=0);
>
>
>In a message dated Mon, 3 Jul 2000  4:50:43 PM Eastern Daylight Time, Robin
Kirkman <misty@drrobin.yi.org> writes:
>
><<
>eck.. ollie's works fine
>
>*(str++)=0;
>
>that sets the value *str to 0, then increments str
>while(*str) *(str++)=0;
>while the value at str is not zero, set the value to zero and increment str
>
>
>
>ComAsYuAre@aol.com wrote:
>>
>> I don't think yours will actually work though--it won't clear the first
char.  Rather, use:
>>
>> for(int i=0; str[i]; str[i++]=0);
>>
>> If you post-increment i in the conditional, it will test before the first
loop and thus skip str[0] in the clearing code.  Incrementing in the
"increment" piece of the for loop generally works better =P
>>
>> In a message dated Mon, 3 Jul 2000  4:10:02 PM Eastern Daylight Time,
Olle Hedman <oh@hem.passagen.se> writes:
>>
>> <<
>> just don't forget to save the stringpointer if you want to use the string
>> variable again :)
>>
>> file://Olle
>>
>> ComAsYuAre@aol.com wrote:
>> > Or if we're going REALLY trying to shorten that code...
>> >
>> > while(*str) *str++=0;
>> >
>> > :-)
>>
>>  >>
>
> >>
>
>
>




Follow-Ups: