[fpc-devel] StrPos giving a access violation

Marc Weustink marc.weustink at cuperus.nl
Fri Dec 9 16:07:57 CET 2005


Graeme Geldenhuys wrote:
> Hi,
> 
> The 3rd unit test fails due to StrPos giving a access violation.  I ran 
> this same unit test under Delphi 7 and it passed as StrPos returned a 
> nil value when passing two '' param values.
> 
> Has this been reported as a bug yet?
> 
> 
> -----------------  CUT  -------------------
> var
>   lFrom: string;
>   lSearch: string;
>   lResult: PChar;
> begin
>   lFrom   := 'abc';
>   lSearch := 'b';
>   lResult := StrPos(Pointer(lFrom), Pointer(lSearch));

Eeeeuwww....

StrPos is expecting 2 PChar params and not a Pointer.
Please use PChar(lFrom) and PChar(lSearch) to let the compiler make the 
proper string to PChar conversion.
You never ever can cast a string to a pchar using Pointer()

Marc



>   AssertEquals('Failing on 1', 'bc', string(lResult));
> 
>   lSearch := 'x';
>   lResult := StrPos(Pointer(lFrom), Pointer(lSearch));
>   AssertEquals('Failing on 2', '', string(lResult));
> 
>   lFrom   := '';
>   lSearch := '';
>   lResult := StrPos(Pointer(lFrom), Pointer(lSearch));
>   AssertEquals('Failing on 3', '', string(lResult));
> 
> -----------------  END  -------------------
> 
> 
> Regards,
>   - Graeme -
> 
> 
> 
> 
> 
> 
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel
> 




More information about the fpc-devel mailing list