On 12/10/2011 05:07 AM, Barry Schwarz wrote:
> On Mon, 5 Dec 2011 09:15:54 -0600 (CST), Modafinil
> <
vanl...@iinet.net.au> wrote:
>
>> Hi,
>>
>> Some code I'm working with uses the C string library function
>> strrchr(), but my compiler's implementation works in a non-standard
>> way. It can't seem to point to a string's first '\0' character with a
>> call like p = strrchr(dest, '\0');
>
> Since a string must contain exactly one '\0' and since that character
> will definitely be the last character in the string, your attempt to
> call strrchr with the first argument pointing to it is pointless.
It's not pointless. It's true that there's other ways of getting the
same result. For example,
for(p=dest; *p; p++);
would be my favorite. However, if done properly (and we're not sure how
he's done it, since he didn't show us the code that failed), it should
produce the result he's asking for. It wastes time doing two comparisons
at each point in the string, rather than just one, so it won't be quite
as fast, but it should work.
> Further more, calling strchr or strrchr with a second argument of zero
> is a waste. The returned value is guaranteed to str+strlen(str).
>
> You seem to be under the impression that the argument passed to
> strrchr should point to the end of the string. This is wrong.
What did he say to give you that impression?
>> In preference to altering the original code, I would like to somehow
>> re-define the function within an associated glue header file.
>> Something along the lines of:
>>
>> #define strrchr(a, b) (&a[strlen(a)])
>> or
>> #define strrchr(a, b) ((a)+strlen((a)))
>
> Before you start inventing macros, it might be a good idea to show us
> some sample code and describe what you want to accomplish.
I agree that sample code is the key thing that's missing; until we see
his code we can't be sure whether there's actually a defect in the
library implementation, or in his code (which seems far more likely).
However, his description seems clear enough to me, and a+strlen(a)
matches that description.
--
James Kuyper