Quote
In article <sundial.1713.01388...@primenet.com> sund...@primenet.com (Sundial Services) writes:
>From: sund...@primenet.com (Sundial Services)
>Subject: Re: array[0..0] of Anything;
>Date: Thu, 30 Nov 1995 14:30:26 MST
>In article <nlawson.1.000AE...@uga.cc.uga.edu> nlaw...@uga.cc.uga.edu (Neal Lawson) writes:
>> I am a "C" programmer learning "Delphi", and I have some questions about
>>Pascal arays, specifically the construct:
>> array[0..0] of Anything;
>>1) What is this creature? I've seen this in function and procedure
>>declarations, and it seems to be used whenever the number of elements in the
>>array is not specified by the "type". In these cases, the array AND the
>>number of elements in the array are passed to the function are procedure.
>>2) Here is a specific example of when this construct is used. The Windows'
>>API function, CreatePalette(), has the following declaration (via Delphi
>>Online Help):
>> function CreatePalette(var LogPalette: TLogPalette): HPalette;
>>where a TLogPalette is:
>> TLogPalette = record
>> palVersion: Word;
>> palNumEntries: Word;
>> palPalEntry: array[0..0] of TPalet{*word*249}try;
>> end;
>>and a TPalet{*word*249}try is:
>> TPalet{*word*249}try = record
>> peRed: Byte;
>> peGreen: Byte;
>> peBlue: Byte;
>> peFlags: Byte;
>> end;
>> QUESTION: How do I create an array of TPalet{*word*249}try's (with say, 10
>> elements) and assign this array to TLogPalette.palPalEntry? No
>> matter what I try, I get a type mismatch.
>Looks like some strange coding practices to me. Presumably the code in
>question is compiled with array-bounds checking turned off...
yes, bounds checking is off.
Quote
>Presumably the structure contains a variable number of TPalet{*word*249}tries,
>beginning with #0 and moving up. The entry is declared as an array starting
>with zero because it is logically an array of identical entries, and the upper
>boundary does not matter because no bounds-checking is being done anyway.
Also correct. I have seen (and tried) a technique for creating
zero-based dynamic arrays in Pascal, and this technique works like this:
(*-------------------*)
type
TAnyArr = array[0..0] of Anything;
PTAnyArr = ^TAnyArr;
var
TArr: PTAnyArr;
begin
(* Allocate an array of Anything's with a range 0 - numitems-1 *)
GetMem(TArr, sizeof(Anything) * numitems);
(* Access the 'index' element of the array *)
something := TArr^[index].whatever;
(* Free the dynamic array *)
FreeMem(TArr, sizeof(Anything) * numitems);
end;
(*--------------------*)
In these cases, the array is always accessed through the POINTER variable, not
an array[0..0] variable. BTW, the above technique looks to be identical to
"Pointer Arithmetic" in "C" (any comments?).
Quote
>You will need to use a loop, not a single assignment, to copy individual
>palette entries into the structure. Be darn sure that there really is enough
>memory in whatever you are copying to; otherwise you will scribble the stack.
>/mr
This is where I have the problem:
1) I can create an array of TPalette either dynamically (using the GetMem
technique above) or on the stack (using a local array); however, I cannot
successfully assign either variable to the TLogPalette.palPalEntry. I have
tried every typecast I can think of. If palPalEntry were a POINTER, this
would work, right?
2) If I assign palette entries in a loop, how do I allocate the memory for
the array? I tried this:
var
aPalette: TLogPalette;
begin
aPalette.palVersion := $300;
aPalette.palNumEntries := 10;
(* This line generates a compile-time type error *)
GetMem(aPalette.palPalEntries, sizeof(TPalet{*word*249}try)*10);
(* This line compiles but doesn't work *)
GetMem(Pointer(aPalette.palPalEntries), sizeof(TPalet{*word*249}try)*10);
end;
*Thank you* to everyone who has responded! I appreciate the help.
Still trying...
-neal