Delphi Error 2010 E2010 Incompatible types: "AnsiChar" and "Char"

I downloaded some old code from Delphi Magazine, and when I compile it in Delphi 2010, I get incompatible types E2010: "AnsiChar" and "Char".

How to fix this error?

pAddr : = inet_ntoa (AddrIn.sin_addr);

pAddr defined as PChar
inet_ntoa is a function that returns PAnsiChar

+4
source share
2 answers

Use AnsiString and String to safely perform the necessary casts.

MyAnsiString := AnsiString(inet_ntoa(AddrIn.sin_addr)); MyString := String(MyAnsiString); pAddr := PChar(MyString); 
+3
source

It depends on what you are trying to do with it. Do you use the address yourself or pass it into external code?

If you use it yourself, try thoiz_vd answer. As a general rule, keep as much of your internal string processing as possible in a string . This will save you a lot of trouble.

On the other hand, if you pass it to an external routine, for example, something in the Windows API, you must make sure that the data is in the format that the API expects. This is slightly less clear than in the first case, because when Delphi switched from AnsiString to UnicodeString as the main string type, they canceled many winapi headers in the Windows module to allow the equivalent widescreen version of the routines that had the strings.

So, check what you are sending it to. If PChar is required for this, use thoiz_vd's answer. But if it expects PAnsiChar , redeclare pAddr as PAnsiChar .

+2
source

All Articles