I'm stuck in this trouble where the compiler says:

Incompatible types: 'AnsiChar' and 'Integer'

to the last element of AnsiChar array, that is a integer that is a null termination. How fix it?

C++ code:

static const BYTE  myarray[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 0 };

// Usage example:
if(Send(s, (char *) myarray, sizeof(myarray), 0) <= 0)

My attempt in Delphi:

  MyArray: array [0 .. MAX_PATH] of AnsiChar = ( 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 0 );
  S: TSocket;

// Usage example:
send(S, MyArray, Length(MyArray), 0);
  • @Rudy It's not the best dupe. I reopened it. Please post your answer. Best explain ordinal char literals too. Thanks. – David Heffernan Apr 15 at 14:04

You can almost define it the way you did:

  MyArray: array[0..MAX_PATH] of AnsiChar = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', #0);

But then you get an error complaining about the number of elements, so you would have to add some 250 extra zeroes to complete it:

// Possible, but not necessary, see below 
  MyArray: array[0..MAX_PATH] of AnsiChar = 
    ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
     'I',  #0,  #0,  #0,  #0, ....
                ...            #0,  #0,  #0); 

This can be done much simpler, however:

  MyArray: array[0..MAX_PATH] of AnsiChar ='ABCDEFGHI';

This special syntax should work in most versions of Delphi and not get a compiler error.

For the length during a send(), you'll have to use StrLen(), not Length():

send(S, MyArray, StrLen(MyArray) + 1, 0);

Alternatively, you can do this:

  Stg: AnsiString;
  Stg :='ABCDEFGHI';
  // Second parameter is untyped const, so use ^
  send(S, PAnsiChar(Stg)^, Length(Stg) + 1, 0);

FWIW, #0 is the character with ordinal value 0. Alternatives are:

^@ (meaning Control+@; ^A = #1 = Chr(1), ^M = #13, etc.)

Each of the above has the same meaning.

  • Isn't Length the length of the array? And so not equal to the sizeof from the C++ code. – David Heffernan Apr 15 at 14:22
  • @David: Length(MyArray) is MAX_PATH+1. Just checked that. Updated my answer. – Rudy Velthuis Apr 15 at 14:35
  • OK, needs to be StrLen() + 1, but I edited that – David Heffernan Apr 15 at 14:39
  • @RudyVelthuis, E2036 Variable required here: PAnsiChar(Stg) on alternative solution of your answer. How fix? – BrowJr Apr 15 at 18:11
  • 3
    Why is MAX_PATH being used at all? In the original C++ code, the array length is only 10 chars max, not the 261 chars that the Delphi code is forcing. Fix the array as MyArray: array [0 .. 9] of AnsiChar, then Sizeof() can be used in the Delphi code instead of using Length() or StrLen()+1 – Remy Lebeau Apr 15 at 20:15

You can use #0 to represent numeric 0 as an AnsiChar. But, the C++ code is assigning char values to a BYTE array, and you can't implicitly assign an AnsiChar to a Byte in Delphi, you need a type-cast.

A literal translation of the C++ code to Delphi would look more like this:

  myarray[0..9] of Byte = (Ord('A'), Ord('B'), Ord('C'), Ord('D'), Ord('E'), Ord('F'), Ord('G'), Ord('H'), Ord('I'), 0);
  // alternatively:
  // myarray[0..9] of AnsiChar = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', #0);

  s: SOCKET;
  // Usage example:
  if (send(s, myarray, SizeOf(myarray), 0) <= 0) then
  • based on my last question about send() a PByte type, here we also can make: send(s, myarray^, SizeOf(myarray), 0)? PS: myarray^ or simply myarray? See this. – BrowJr Apr 16 at 12:05
  • 1
    @BrowJr you can't use ^ to dereference a fixed array in Delphi, only a pointer, and there is no need to use a pointer in this example, in fact there is no need to dereference the fixed array at all – Remy Lebeau Apr 16 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.