[fpc-pascal] Questions regarding arrays

Graeme Geldenhuys graemeg.lists at gmail.com
Wed Sep 22 11:25:29 CEST 2010


I'm have some tough times with arrays (I use them very little). With
trial and error I found that to use a pointer to an array of int32
values returned by a C API (Xlib to be exact), I have to define it as
follows in Object Pascal.

  TAtomArray = array[0..0] of TAtom;
  PAtomArray = ^TAtomArray;

  xdndtypes: PAtomArray;
  a: TAtom;  // just a culong type
    XGetWindowProperty(Display, FSrcWinHandle,
        XdndTypeList, 0, 16000,
        @actualtype, @actualformat, @count, @remaining,
    // 'count' tells me the amount of items in the returned xdndtypes array
    // so I can access the array items as follows
    a := xdndtypes^[i]

Now if I change TAtomArray to the follow, then my code doesn't work. :-)

  TAtomArray = array of TAtom;   // a dynamic array

So what exactly is the difference between these two?

    TAtomArray = array[0..0] of TAtom;
    TAtomArray = array of TAtom;

Is array[0..0] not actually a dynamic array, but just a static array
with a single item? If so, then why does the returned value from the C
API call, which returns a pointer to an array of culong's (normally
more that one item) work? Somewhere I'm getting confused with all
this, but would love to understand it correctly, and why it works. :)

>From the XGetWindowProperty() man page, it says the the following
about the return type which get stored in xdndtypes value (the last
parameter in the declaration below).

       int XGetWindowProperty(Display *display, Window w, Atom
property, long long_offset, long
              long_length, Bool delete, Atom req_type, Atom
*actual_type_return, int *actual_for‐
              mat_return, unsigned long *nitems_return, unsigned long
              unsigned char **prop_return);

"If the returned format is 32, the property data will be stored as an
array of longs (which in a 64-bit application will be 64-bit values
that are padded in the upper 4 bytes)."

  - Graeme -

fpGUI - a cross-platform Free Pascal GUI toolkit

More information about the fpc-pascal mailing list