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

Paul M Foster paulf at quillandmouse.com
Sun Feb 19 00:33:43 CST 2006


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




More information about the Discuss mailing list