By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,888 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,888 IT Pros & Developers. It's quick & easy.

Questions about a Windows struct

P: n/a
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;

and if I have a pointer to some allocated memory BYTE * p;
can I assign it like this: Buffer[0] = p;

I'm actually accessing this C struct from another language and
my C is quite rusty so sorry for the dumb question.

Regards
Chris Saunders

Aug 29 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"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"
Aug 29 '08 #2

P: n/a
"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. Buffer[0] is a BYTE so a pointer to it is of type BYTE *.
and if I have a pointer to some allocated memory BYTE * p;
can I assign it like this: Buffer[0] = p;
Woah... back up. This looks like the "struct hack" (GIYF). When the
last member of a struct is a one-element array, it is often that case
that we are to assume that the whole struct has been "over
allocated". Rather than allocating just the room for the declared
members, the programmer will have allocated that size + some more for
the buffer at the end.

Now, to get to your question, no, you can't allocate memory to a
buffer by assigning to Buffer[0]. For one thing, all the types are
wrong. What usually happens is that memory obtained from some place
is simply treated as if it were one of these structs, the buffer being
accessed in the normal way but with indexes (or pointers) beyond the
otherwise rather pointless single declared element.

This is about as much as C has to say on the matter (other than that
this trick is, strictly speaking, not permitted).

--
Ben.
Aug 29 '08 #3

P: n/a
First, thanks very much for the reply Keith. I had a look at the FAQ but I
still don't see how to allocate some memory and assign it to Buffer[0].
Here is the struct again:

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;

Regards
Chris Saunders

Aug 29 '08 #4

P: n/a
Chris Saunders wrote:
First, thanks very much for the reply Keith.
Which you should have quoted!
I had a look at the FAQ
but I still don't see how to allocate some memory and assign it to
Buffer[0].
You can't and you wouldn't want to. buffer[0] is a single BYTE.

--
Ian Collins.
Aug 29 '08 #5

P: n/a
My thanks to all who replied. I'm starting to get this.

Regards
Chris Saunders
Aug 29 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.