3

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 };
SOCKET s;

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

My attempt in Delphi:

var
  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);
1
  • @Rudy It's not the best dupe. I reopened it. Please post your answer. Best explain ordinal char literals too. Thanks. Commented Apr 15, 2019 at 14:04

2 Answers 2

7

You can almost define it the way you did:

var
  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 
var
  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:

var
  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:

var
  Stg: AnsiString;
begin
  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:

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

Each of the above has the same meaning.

Sign up to request clarification or add additional context in comments.

9 Comments

Isn't Length the length of the array? And so not equal to the sizeof from the C++ code.
@David: Length(MyArray) is MAX_PATH+1. Just checked that. Updated my answer.
@RudyVelthuis, E2036 Variable required here: PAnsiChar(Stg) on alternative solution of your answer. How fix?
I did not know that the second parameter of send is a var parameter (probably an untyped var parameter?). In that case, you'll have to pass PAnsiChar(Stg)^ (note the added ^).
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
|
3

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:

const
  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);

var
  s: SOCKET;
begin
  ...
  // Usage example:
  if (send(s, myarray, SizeOf(myarray), 0) <= 0) then
    Exit;
  ...
end;

2 Comments

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 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.