Board index » delphi » Equivalent of C 'static'? Easy init for arrays??

Equivalent of C 'static'? Easy init for arrays??

Quote
In article <31bdd4f7.14942...@news.demon.co.uk> Al...@agreenwo.demon.co.uk (Alan Greenwood) writes:
>From: Al...@agreenwo.demon.co.uk (Alan Greenwood)
>Subject: Equivalent of C 'static'? Easy init for arrays??
>Date: Tue, 11 Jun 1996 20:49:44 GMT
>As a renegade C programmer recently converted to Delphi, I'm wondering
>if there is a Delphi equivalent for the C keyword static. This
>directive forces permanent storage for the variable declared, and
>ensures that its value in a function remains stable between calls.

any variable not in the local scope of a proc eg decalred in the main body of
a unit will persist.

Quote
>I've also been puzzled by the apparent absence of an easy way of
>declaring AND intitialising pointers to (null-terminated) strings at
>the same time. In C if you want to declare such an array you type -
>char *Array[] = { "abc", "def", "ghi", "jkl" };

type
        TestCharArray = array[ 0..3 ] of string[3];

const
       CharArray: TestCharArray = ( 'abc', 'def', 'ghi', 'jkl' );

Imho, this means that you can at least read the code !

HTH

boris

 

Re:Equivalent of C 'static'? Easy init for arrays??


As a renegade C programmer recently converted to Delphi, I'm wondering
if there is a Delphi equivalent for the C keyword static. This
directive forces permanent storage for the variable declared, and
ensures that its value in a function remains stable between calls.

I've also been puzzled by the apparent absence of an easy way of
declaring AND intitialising pointers to (null-terminated) strings at
the same time. In C if you want to declare such an array you type -

char *Array[] = { "abc", "def", "ghi", "jkl" };

and the compiler works out that a) there are 4 elements in the array
and b) that the values in the curly braces are the inital values in
that array. In Delphi it appears that the same result is much more
difficult to achieve -

type
        TestCharArray = array[ 0..3 ] of char;

var
        CharArray: TestCharArray;

procedure SomeProcedureOrOther;
begin
        CharArray[ 0 ] := 'abc';
        CharArray[ 1 ] := 'def';
        CharArray[ 2 ] := 'ghi';
        CharArray[ 3 ] := 'jkl';
end;

In other words a) it is much more difficult (think of doing this with
an array of 50 elements!!) and b) it is done at runtime. I hope, of
course, that I'm wrong - any ideas, anyone?

Thanks in advance.

Alan Greenwood
--

Re:Equivalent of C 'static'? Easy init for arrays??


Quote
Alan Greenwood wrote:

 >
 > As a renegade C programmer recently converted to Delphi, I'm
wondering
 > if there is a Delphi equivalent for the C keyword static. This
 > directive forces permanent storage for the variable declared, and
 > ensures that its value in a function remains stable between calls.
 >
 > I've also been puzzled by the apparent absence of an easy way of
 > declaring AND intitialising pointers to (null-terminated) strings at
 > the same time. In C if you want to declare such an array you type -
 >
 > char *Array[] = { "abc", "def", "ghi", "jkl" };
 >
 > and the compiler works out that a) there are 4 elements in the array
 > and b) that the values in the curly braces are the inital values in
 > that array. In Delphi it appears that the same result is much more
 > difficult to achieve -
 >
 > type
 >         TestCharArray = array[ 0..3 ] of char;
 >
 > var
 >         CharArray: TestCharArray;
 >
 > procedure SomeProcedureOrOther;
 > begin
 >         CharArray[ 0 ] := 'abc';
 >         CharArray[ 1 ] := 'def';
 >         CharArray[ 2 ] := 'ghi';
 >         CharArray[ 3 ] := 'jkl';
 > end;
 >
 > In other words a) it is much more difficult (think of doing this with
 > an array of 50 elements!!) and b) it is done at runtime. I hope, of
 > course, that I'm wrong - any ideas, anyone?
 >
 > Thanks in advance.
 >
 > Alan Greenwood
 > --

Alan,

As for your first question (C Static Equiv), there is a solution try
this.

const Identifier: StorageClass

Ex: const i: integer;  {I believe i is initialized to Zero}

For your second question there is a slight solution.  The problem is
that you need to know the size of the array at compile time.  Bottom
line no dynamic pre-initialized arrays.  You must also initialize every
subscript!

const a: array[0..1] of string = ?'String1','String2'?;

where the "?" are, cant remember if they are brackets ([]), Braces ({}),
or parens ( () ).  Maybe a "set" might work for your case???  If you
have the VCL source code there are lots of example in there.  Borland
seems to like to declare local static arrays for fast table lookup.  If
this doesn't get it e-mail me and I will take a look at the VCL Source.
I don't have Delphi with me so this is all off-the-cuff.

Hope this helps,

-Tim

Re:Equivalent of C 'static'? Easy init for arrays??


Quote
Boris Ingram wrote:

> In article <31bdd4f7.14942...@news.demon.co.uk> Al...@agreenwo.demon.co.uk (Alan Greenwood)
writes:
> >From: Al...@agreenwo.demon.co.uk (Alan Greenwood)
> >Subject: Equivalent of C 'static'? Easy init for arrays??
> >Date: Tue, 11 Jun 1996 20:49:44 GMT

> >As a renegade C programmer recently converted to Delphi, I'm wondering
> >if there is a Delphi equivalent for the C keyword static. This
> >directive forces permanent storage for the variable declared, and
> >ensures that its value in a function remains stable between calls.

> any variable not in the local scope of a proc eg decalred in the main body of
> a unit will persist.

However such a variable is accessible to all functions in the unit.
C-style 'static' variables can be declared in local scope. Any Pascal
equivalent?

Re:Equivalent of C 'static'? Easy init for arrays??


In article <31bdd4f7.14942...@news.demon.co.uk>,
   Al...@agreenwo.demon.co.uk (Alan Greenwood) wrote:

Quote
>As a renegade C programmer recently converted to Delphi, I'm wondering
>if there is a Delphi equivalent for the C keyword static. This
>directive forces permanent storage for the variable declared, and
>ensures that its value in a function remains stable between calls.

>I've also been puzzled by the apparent absence of an easy way of
>declaring AND intitialising pointers to (null-terminated) strings at
>the same time. In C if you want to declare such an array you type -

>char *Array[] = { "abc", "def", "ghi", "jkl" };

>and the compiler works out that a) there are 4 elements in the array
>and b) that the values in the curly braces are the inital values in
>that array. In Delphi it appears that the same result is much more
>difficult to achieve -

>type
>    TestCharArray = array[ 0..3 ] of char;

>var
>    CharArray: TestCharArray;

>procedure SomeProcedureOrOther;
>begin
>    CharArray[ 0 ] := 'abc';
>    CharArray[ 1 ] := 'def';
>    CharArray[ 2 ] := 'ghi';
>    CharArray[ 3 ] := 'jkl';
>end;

>In other words a) it is much more difficult (think of doing this with
>an array of 50 elements!!) and b) it is done at runtime. I hope, of
>course, that I'm wrong - any ideas, anyone?

How about :

Const
  CharArray : Array[0..49] Of String[3] = (
    'abc',
    'def',
    'ghi',
    'jkl',
    .
    .
    .
    'xyz');

-----------------------------------------------
Mike Chapin
Powder River
mcha...@vcn.com
http://www.vcn.com/server/netizens/mchapin/first.html
Gillette, WY

Not the end of the earth but you can see it from
there.
-----------------------------------------------

Re:Equivalent of C 'static'? Easy init for arrays??


If you want a local variable to persist between calls to a function or
procedure declare it as a typed constant, eg:

Procedure TestStatic(Var Anything : real);
 Const StarticVar:longint = 10;
  Begin
   ....
  end;

Hope this helps;

Quote
> jeremy <jer...@xyris.com> wrote in article
> > any variable not in the local scope of a proc eg decalred in the main
body of
> > a unit will persist.

> However such a variable is accessible to all functions in the unit.
> C-style 'static' variables can be declared in local scope. Any Pascal
> equivalent?

Re:Equivalent of C 'static'? Easy init for arrays??


Quote
In article <31bdd4f7.14942...@news.demon.co.uk>, Al...@agreenwo.demon.co.uk (Alan Greenwood) writes:
>As a renegade C programmer recently converted to Delphi, I'm wondering
>if there is a Delphi equivalent for the C keyword static. This
>directive forces permanent storage for the variable declared, and
>ensures that its value in a function remains stable between calls.

Welcome to the fold! Never fear, you won't regret your expatriation
(not much anyway ;-).

The equivalent in Borland's dialect of Pascal is called a "typed
constant"; it's not part of standard Pascal and is most decidedly not
portable, not that much Delphi code (or Windows code in general, for
that matter) is anyway.

You declare such a beast like so:

procedure MyProc ;
const
  StaticValue : integer = 0 ;
begin
  Inc( StaticValue ) ; { or whatever, the value of StaticValue is
                         maintained across calls to MyProc }
end ;

Quote
>I've also been puzzled by the apparent absence of an easy way of
>declaring AND intitialising pointers to (null-terminated) strings at
>the same time. In C if you want to declare such an array you type -

>char *Array[] = { "abc", "def", "ghi", "jkl" };

>and the compiler works out that a) there are 4 elements in the array
>and b) that the values in the curly braces are the inital values in
>that array. In Delphi it appears that the same result is much more
>difficult to achieve -

>type
>    TestCharArray = array[ 0..3 ] of char;

>var
>    CharArray: TestCharArray;

>procedure SomeProcedureOrOther;
>begin
>    CharArray[ 0 ] := 'abc';
>    CharArray[ 1 ] := 'def';
>    CharArray[ 2 ] := 'ghi';
>    CharArray[ 3 ] := 'jkl';
>end;

You're still a bit hazy on Pascal array declaration notation, to get a
2d array like this you need either
  TestCharArray : array [0..3] of array [0..3] of char ;
or
  TestCharArray : array [0..3, 0..3] of char ;

either produces the same data structure.

Also, you're still short your Null terminator characters; Pascal doesn't
automagically append them like C does; to get them do something like:

...   CharArray[ 0 ] := 'abc'#0 ; ...

#0 is Borlandish shorthand for ASCII 0, the following will also work:  

...   CharArray[ 0 ] := 'abc' + Chr( 0 ) ;

However, the typed constant mechanism also allows for what amounts to
preinitialized variables like this:

const
  TestCharArray : array[ 0..3, 0..3 ] of char =
   ( 'abc'#0, 'def'#0, 'ghi'#0, 'jkl'#0 ) ;

Quote
>In other words a) it is much more difficult (think of doing this with
>an array of 50 elements!!) and b) it is done at runtime. I hope, of
>course, that I'm wrong - any ideas, anyone?
>Thanks in advance.
>Alan Greenwood

HTH

-* Stephen *-
Stephen Posey
University of New Orleans
Email  : S...@uno.edu
WWW    : http://www.uno.edu/~slp

Re:Equivalent of C 'static'? Easy init for arrays??


You can declare an identifier as const and use it as a variable??? What gives?

Quote
Johan Kotze wrote:

> If you want a local variable to persist between calls to a function or
> procedure declare it as a typed constant, eg:

> Procedure TestStatic(Var Anything : real);
>  Const StarticVar:longint = 10;
>   Begin
>    ....
>   end;

> Hope this helps;

> > jeremy <jer...@xyris.com> wrote in article

> > > any variable not in the local scope of a proc eg decalred in the main
> body of
> > > a unit will persist.

> > However such a variable is accessible to all functions in the unit.
> > C-style 'static' variables can be declared in local scope. Any Pascal
> > equivalent?

Re:Equivalent of C 'static'? Easy init for arrays??


Thanks very much to all the people who posted/emailed answers to my
questions about the C keyword 'static' and the initialisation of
arrays.

Regards to all
Alan Greenwood
--

Re:Equivalent of C 'static'? Easy init for arrays??


Mr. Greenwood,

You can:

Type
  TestCharArray = Array [0..3] of String;

Const
  CharArray : TestCharArray = ('abc', 'def', 'ghi', 'jkl');

Note: Pascal is strongly typed. So, merely saying:

Const
  CharArray = ('abc',  .... );

will not make the compiler happy. Remember, Delphi is a 1-pass
compiler unlike C or C++. If you have ever designed compilers youll
realize why there are some things you can't DIRECTLY translate from
C/C++ to Pascal.

later,

bly

Quote
Al...@agreenwo.demon.co.uk (Alan Greenwood) wrote:
>As a renegade C programmer recently converted to Delphi, I'm wondering
>if there is a Delphi equivalent for the C keyword static. This
>directive forces permanent storage for the variable declared, and
>ensures that its value in a function remains stable between calls.
>I've also been puzzled by the apparent absence of an easy way of
>declaring AND intitialising pointers to (null-terminated) strings at
>the same time. In C if you want to declare such an array you type -
>char *Array[] = { "abc", "def", "ghi", "jkl" };
>and the compiler works out that a) there are 4 elements in the array
>and b) that the values in the curly braces are the inital values in
>that array. In Delphi it appears that the same result is much more
>difficult to achieve -
>type
>    TestCharArray = array[ 0..3 ] of char;
>var
>    CharArray: TestCharArray;
>procedure SomeProcedureOrOther;
>begin
>    CharArray[ 0 ] := 'abc';
>    CharArray[ 1 ] := 'def';
>    CharArray[ 2 ] := 'ghi';
>    CharArray[ 3 ] := 'jkl';
>end;
>In other words a) it is much more difficult (think of doing this with
>an array of 50 elements!!) and b) it is done at runtime. I hope, of
>course, that I'm wrong - any ideas, anyone?
>Thanks in advance.
>Alan Greenwood
>--

Re:Equivalent of C 'static'? Easy init for arrays??


Quote
On Fri, 14 Jun 1996 09:11:34 -0400, jeremy <jer...@xyris.com> wrote:
>You can declare an identifier as const and use it as a variable??? What gives?

Yes, {*word*269}ia - oops, Jeremy, there is a Santa Claus. Unfortunately this one
will go away in Delphi V2. In Borland/Turbo Pascal and Delphi V1 a "constant" is
really a variable with an initialized value and you can change it at will. Once
you go to Delphi V2 you can no longer change it. It is a true constant.

Thus Mr. Kotze's method will only work for your 16 bit apps.

dick MacDonald

Re:Equivalent of C 'static'? Easy init for arrays??


Quote
s...@uno.edu wrote:
>In article <31bdd4f7.14942...@news.demon.co.uk>, Al...@agreenwo.demon.co.uk (Alan Greenwood) writes:
>>I've also been puzzled by the apparent absence of an easy way of
>>declaring AND intitialising pointers to (null-terminated) strings at
>>the same time. In C if you want to declare such an array you type -

>etc...

try this:

const
   Images: array[0..1] of PChar = ('GREENLGT', 'REDLGT');

This will create an array of 2 null terminated strings with the values
"GREENLGT" and "REDLGT".  I use this type of setup for reading bitmaps
from RES files.
-------------------------------
Thomas Paul
TOMG...@Pipeline.Com
Dean Witter, Discover & Co.
New York City, NY USA
----------------------------------

Re:Equivalent of C 'static'? Easy init for arrays??


Quote
>{*word*106}...@ix.netcom.com (Richard MacDonald) wrote in article

<31c57343.142076...@nntp.ix.netcom.com>...
Quote
> On Fri, 14 Jun 1996 09:11:34 -0400, jeremy <jer...@xyris.com> wrote:

> >You can declare an identifier as const and use it as a variable??? What
gives?

> Yes, {*word*269}ia - oops, Jeremy, there is a Santa Claus. Unfortunately this
one
> will go away in Delphi V2. In Borland/Turbo Pascal and Delphi V1 a
"constant" is
> really a variable with an initialized value and you can change it at
will. Once
> you go to Delphi V2 you can no longer change it. It is a true constant.

<snip>
I just tried this with D2 C/S.  And it works just like it always did.
Here's my code:

procedure TForm1.Button1Click(Sender: TObject);

const
testvar : integer = 30;

begin
label1.caption := inttostr(testvar);
dec(testvar);
end;

Re:Equivalent of C 'static'? Easy init for arrays??


Actually there are two type of "constants" in Delphi.  One is an untyped constant which is a true
constant.

const
  ParagraphSize = 16;

The compiler, in this case, actually replaces all instances of ParagraphSize with 16 during the compile.
 No memory is associated with the constant.  

The other type of constant is a typed constant.  As you demonstrated, a typed constant is just a
variable that is initialized to a value.  

const
  ParagraphSize : Word = 16;

In the new delphi 2.0,  you can now have directly initialized variables and this is the preferred method
of initializing a variable.

var
  ParagraphSize : Word = 16;

There is a compiler option to allow the modification of typed constants (cant remember at the moment -
maybe $T???).  But it is simpler to used initialized variables.

--

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
               Richard Burroughs
            Hurricane Software, Inc.
               rburr...@tfs.net
              Authors of WinGREP
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_

Other Threads