"Chris Saunders" <ev**@mountaincable.netwrites:
Here is the C declaration of a struct from Windows:
typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT {
union {
//
// Used to return the required buffer size if return code is
STATUS_BUFFER_OVERFLOW
//
DWORD BufferLength;
//
// On success the data is copied here.
//
BYTE Buffer[1];
} DUMMYUNIONNAME;
} TXFS_READ_BACKUP_INFORMATION_OUT, *PTXFS_READ_BACKUP_INFORMATION_OUT;
Can I declare a pointer to Buffer[0] as BYTE ** ptr;
No. A BYTE** is a pointer to a pointer to a BYTE. There has to be a
pointer-to-BYTE object for it to point to. There is no
pointer-to-BYTE object in the code you've shown us.
Given an object obj of type TXFS_READ_BACKUP_INFORMATION_OUT, the
expression obj.DUMMYUNIONNAME.Buffer, which is an array name,
evaluates (in most contexts) to a *value* of type pointer-to-BYTE, but
there's no object of type pointer-to-BYTE.
Buffer is an array. If you want to point to the array object, you
need something of type BYTE(*)[1], i.e., a pointer to an array of one
BYTE. But that's almost certainly not what you want. Instead, you
probably want a pointer to *the first element* of Buffer; for that,
you just need a BYTE*.
In general, a FOO* that points to the first element of an array of FOO
is the most common way to access the elements of the array.
It looks like this code probably does something very similar to the
struct hack (see
www.c-faq.com question 2.6), in addition to using a
union to overlay the buffer length (on input?) with the buuffer itself
(on output?).
and if I have a pointer to some allocated memory BYTE * p;
can I assign it like this: Buffer[0] = p;
No. Buffer[0] is of type BYTE; p is of type BYTE*.
You can write Buffer[0] = *p to copy a single BYTE. To copy multiple
bytes, use a loop or memcpy() -- but make sure there's enough space
allocated to hold it.
I'm actually accessing this C struct from another language and
my C is quite rusty so sorry for the dumb question.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"