3

In C, I can declare and initialize an char array like this:

char arg[10] = "ANY";

Is there any short syntax to do the same in delphi?

2
  • 1
    You don't typically use C strings in Delphi unless doing interop. You normally just use Delphi strings. Why would you want a character array in Delphi? Commented Feb 28, 2011 at 10:00
  • It sounds like the OP is trying to translate his knowledge of K&R C into Delphi. Commented Mar 1, 2011 at 17:46

2 Answers 2

5

A constant:

const
  arg: array[0 .. 9] of AnsiChar = 'ANY';

A local variable:

var
  arg: array[0 .. 9] of AnsiChar;
...
  arg := 'ANY';

A global variable:

var
  arg: array[0 .. 9] of AnsiChar = 'ANY';
Sign up to request clarification or add additional context in comments.

2 Comments

Opps, my fault. It works, I've used PAnsichar instead of Ansichar :)
People should be aware that this code is intentionally creating an array of AnsiChar, which would be an unusual thing to do. In a unicode delphi application that doesn't have to have some string 'ANY' be ansi, you would normally declare just const arg = 'ANY'. (Unicode string constant).
-1

something like this:

var arg1: string = 'any';

or

var arg2: packed array[0..9] of char = 'any';

if you really want an array starting with index 0, as in C, or

var arg3: pchar = 'any';

if you want a \0 terminated string

Comments

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.