[MAYBE] Re: [NTLUG:Discuss] OT C question

Fred James fredjame at fredjame.cnc.net
Sun Feb 19 13:33:22 CST 2006


Paul M Foster wrote:

> Fred James wrote:
>
>> All
>> Thanks to your help I was able to replace
>>
>>        for ( i = 0; i < Max; i++ ) {
>>                if ( s[i] == S[Sptr] ) {
>>                        if ( i == Limit ) {
>>                                S[Sptr] = s[0];
>>                        }
>>                        else {
>>                                S[Sptr] = s[i+1];
>>                        }
>>                        i = Max;
>>                }
>>        }
>>
>> with
>>
>>        S[Sptr] = *((strchr(s,S[Sptr]))+1);
>>
>> ... where s and S are each a char *, and Sptr is an int.
>>
>
> Hmm. I hope you surrounded the expression with some sort of limiting 
> loop, since if strchr(s, S[Sptr]) doesn't find a match, it will return 
> NULL, and your expression will be equivalent to
>
> S[Sptr] = *(1)
>
> which would return the contents of memory address 0x0001, undoubtedly 
> outside the range of our code, which could cause a segfault. 
> Obviously, I'm looking at an excerpt of a fraction of your code, so 
> take that with a grain of salt.
>
Paul M Foster
Good salt ... s represents the list of allowable characters for this 
application, and there is a function (call it function A) that insures 
the S contains only allowable characters before it is presented to the 
function (call it function B) that contains this line of code.  Function 
A is called only once, and function B potentially many times.
Regards
Fred James





More information about the Discuss mailing list