-3

I recently ran into this data type mismatch. This is I never saw before. I hope someone could explain what these are and how are they different.

Error I got was F2063. [DCC Error] E2010 Incompatible types: 'AnsiChar' and 'Char'

ThN
  • 3,235
  • 3
  • 57
  • 115
  • 3
    Did you try Googling for that error message? Or pressing F1 with the error message highlighted in the messages window? Or try looking in the documentation for [AnsiChar](http://docwiki.embarcadero.com/Libraries/XE7/en/System.AnsiChar) and [Char](http://docwiki.embarcadero.com/Libraries/XE7/en/System.Char)? – Ken White Oct 20 '14 at 18:25
  • near related : [Difference between PAnsiChar and PChar](http://stackoverflow.com/questions/1803426/difference-between-pansichar-and-pchar) – bummi Oct 20 '14 at 18:29
  • Too bad EMBA goofed the clear difference between fundamental and generic types.... – Free Consulting Oct 20 '14 at 19:18
  • So, if temp=10, is Char(10) same as Ansichar(temp)? – ThN Oct 20 '14 at 19:33
  • @ThN not at all. They are different types. Do you know anything about Unicode? – David Heffernan Oct 20 '14 at 19:35
  • 1
    Every Delphi developer should know this, no matter what version they're using. – Jerry Dodge Oct 20 '14 at 19:50
  • 2
    I'm not sure it is correct to mark this as a duplicate. A specific question about a specific compiler error is a different question than the much broader "What's the difference between WideChar and ANSIChar ?", even though the answer to the latter is involved in understanding the answer to the former. It's like "Why won't my car start ?" vs "What's the difference between coal and petroleum?" :) – Deltics Oct 20 '14 at 20:01
  • @Deltics Maybe you could ask on meta. I think this is as clear a dupe as there can be. You disagree. It would be useful to know what the meta folks think. – David Heffernan Oct 20 '14 at 20:10
  • And there are so many dupes of this question. Try this websearch: *E2010 ansichar char site:stackoverflow.com* – David Heffernan Oct 20 '14 at 20:16
  • @JerryDodge I do. The reason I even posted this question was because I never ran into such error before. I am sending a byte of character as CHAR through serial port via variable. This procedure I use and used accepted CHAR parameter type in Delphi 2007. Now, I am up to 2010. Compiler is raising these errors. The problem is this I can successfully send Char type for literal value like Char(13), but it fails when (say WORD temp=13) I pass char(temp) and requests that the parameter must be Ansichar type. I make the change and program complies successfully, but receiving hardware fails. – ThN Oct 20 '14 at 20:17
  • @ThN What you don't seem to realise is that on D2007, `Char` is an alias for `AnsiChar` which is a single byte. And on D2010, `Char` is an alias for `WideChar` which is two bytes. Until you slow down and read the answers to this and other questions, read Marco's whitepaper, and truly understand the huge difference between `AnsiChar` and `WideChar` you are not going to make progress. You won't solve this problem by changing things until the compiler is happy, and hoping that it might work. You need to gain understanding. – David Heffernan Oct 20 '14 at 20:30
  • @David - no biggie really, it just struck me that the questions were actually substantively different, even if the answers were very similar. :shrug: – Deltics Oct 20 '14 at 23:33
  • http://stackoverflow.com/search?q=[delphi]+ansichar+char – J... Oct 21 '14 at 09:08
  • http://stackoverflow.com/questions/24477858/delphi-2010-error-e2010-incompatible-types-char-and-ansichar – J... Oct 21 '14 at 09:08
  • http://stackoverflow.com/questions/22593501/solve-e2010-incompatible-types-ansichar-and-char – J... Oct 21 '14 at 09:09
  • http://stackoverflow.com/questions/6839019/delphi-2010-error-e2010-incompatible-types-ansichar-and-char ...adding to fill the 'linked' sidebar – J... Oct 21 '14 at 09:10
  • Is there anyway this question can be closed or deleted at this point in time? – ThN Oct 23 '14 at 13:42

1 Answers1

8

Historically in Delphi, the Char type was effectively a synonym for the ANSIChar type. That is, a single byte representing a character from an ANSI codepage. NOTE: This is a simplification that ignores the complications arising from multibyte characters which could be encountered in an ANSI string but will suffice for this answer.

This corresponded with the fact that the String type was effectively a synonym for ANSIString.

In Delphi 2009 onward, this changed.

With Delphi 2009, the String and Char types became synonyms for UnicodeString (a WideString with additional capabilities) and WideChar, respectively, reflecting the transition to Unicode as the native format for string and character types. A WideChar is a 2 byte value representing a single character of Unicode (or one half of a surrogate pair).

Therefore, in versions of Delphi prior to Delphi 2009, the following two variables were of compatible types:

var
  ach: ANSIChar;
  ch: Char;         // Synonymous with ANSIChar

However, in Delphi 2009 and later the meaning of the "ch" declarations changes:

var
  ach: ANSIChar;
  ch: Char;         // Synonymous with WIDEChar

As a result, the ach and ch variables are no longer of compatible types.

i.e. the reason you are getting this error is that you have some code which has been declared with ANSIChar types and other code which is using values declared of type Char. When compiled with an old version of Delphi where Char = ANSIChar, the two sets of code are compatible, but in Delphi 2009 and later Char = WideChar and so the two types (Char and ANSIChar) are not compatible.

Deltics
  • 22,162
  • 2
  • 42
  • 70