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