4
votes

I loaded up some old code from Delphi Magazine and when I compile it inside Delphi 2010 I get an E2010 Incompatible types: 'AnsiChar' and 'Char'.

How do I resolve this error?

pAddr := inet_ntoa(AddrIn.sin_addr);

pAddr defined as PChar
inet_ntoa is a function that returns PAnsiChar

2

2 Answers

3
votes

Use an AnsiString and a String to safely perform the necessary casts.

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

That depends on what you're trying to do with it. Are you using the address yourself, or are you passing it to external code?

If you're using it yourself, try thoiz_vd's answer. As a general rule, keep as much of your internal string processing on the string type as you can. It'll save you a lot of hassle.

On the other hand, if you're passing it to an external routine, like something in the Windows API, you have to make sure the data is in the format that the API is expecting. This is a bit less clear-cut than the first case, because when Delphi transitioned from AnsiString to UnicodeString as the fundamental string type, they redid a lot of the winapi headers in the Windows unit to resolve to equivalent widechar versions of the routines that took strings.

So check what you're sending it to. If it requires a PChar, use thoiz_vd's answer. But if it's expecting a PAnsiChar, redeclare pAddr as PAnsiChar.