Assigning a pointer to an array

C

Chris Saunders

My C skills are rather meager so forgive me if I do not express my question
clearly enough. Here is a struct that is declared in Windows:

typedef struct _REPARSE_GUID_DATA_BUFFER
{
DWORD ReparseTag;
WORD ReparseDataLength;
WORD Reserved;
GUID ReparseGuid;
struct {BYTE DataBuffer[1]; } GenericReparseBuffer;
} REPARSE_GUID_DATA_BUFFER, *PREPARSE_GUID_DATA_BUFFER;

Now I'm going to show my attempt at assigning to "DataBuffer". This code is
written in Eiffel but the part inside of the square brackets ([]) is C code
except you will see some "$" symbols which you can ignore:

set_c_generic_reparse_buffer_data_buffer_pointer (a_item, v: POINTER) is
external
"C inline use <Windows.h>"
alias
"[
{
PREPARSE_GUID_DATA_BUFFER itm;
BYTE* db;
itm = $a_item;
db = $v;
&((itm->GenericReparseBuffer).DataBuffer) = db;
}
]"
end

Line 5 of the C code part is where I attempt to assign the array
"DataBuffer[]" in the struct "GenericReparseBuffer" which is a member of the
struct "REPARSE_GUID_DATA_BUFFER". My Eiffel compiler translates the Eiffel
code into C and uses MSVC++ to compile it. The C compiler gives me this
error:

error C2106: '=' : left operand must be l-value

I'm hoping I can get some assistance with this. Sorry about the Eiffel code
but this is a problem with the C part.

Regards
Chris Saunders
 
J

Jens Thoms Toerring

Chris Saunders said:
My C skills are rather meager so forgive me if I do not express my question
clearly enough. Here is a struct that is declared in Windows:
typedef struct _REPARSE_GUID_DATA_BUFFER
{
DWORD ReparseTag;
WORD ReparseDataLength;
WORD Reserved;
GUID ReparseGuid;
struct {BYTE DataBuffer[1]; } GenericReparseBuffer;
} REPARSE_GUID_DATA_BUFFER, *PREPARSE_GUID_DATA_BUFFER;
Now I'm going to show my attempt at assigning to "DataBuffer". This code is
written in Eiffel but the part inside of the square brackets ([]) is C code
except you will see some "$" symbols which you can ignore:
set_c_generic_reparse_buffer_data_buffer_pointer (a_item, v: POINTER) is
external
"C inline use <Windows.h>"
alias
"[
{
PREPARSE_GUID_DATA_BUFFER itm;
BYTE* db;
itm = $a_item;
db = $v;
&((itm->GenericReparseBuffer).DataBuffer) = db;
}
]"
end
Line 5 of the C code part is where I attempt to assign the array
"DataBuffer[]" in the struct "GenericReparseBuffer" which is a member of the
struct "REPARSE_GUID_DATA_BUFFER". My Eiffel compiler translates the Eiffel
code into C and uses MSVC++ to compile it. The C compiler gives me this
error:
error C2106: '=' : left operand must be l-value

The line

&((itm->GenericReparseBuffer).DataBuffer) = db;

doesn't make sense. You basically try to say that the address of
of the 'DataBuffer' array is supposed to be the same as the one
strored in 'db'. But that can't work. 'DataBuffer' is part of the
struct and you can't say it should suddenly be somewhere else.
It's not really clear to me if you want to assign what 'db' is
pointing to to that array, in which case you would have to use

itm->GenericReparseBuffer.DataBuffer[ 0 ] = *db;

But I don't see why you would defined 'DataBuffer' as an array
with a single element if you want store the value of a single
BYTE there.

It could also be that you in principle mean what you're saying
with the "make the address the same as that of 'db'. In that case
your structure isn't suitable for the purposse, you would need

typedef struct _REPARSE_GUID_DATA_BUFFER
{
DWORD ReparseTag;
WORD ReparseDataLength;
WORD Reserved;
GUID ReparseGuid;
struct { BYTE *DataBuffer; } GenericReparseBuffer;
} REPARSE_GUID_DATA_BUFFER, *PREPARSE_GUID_DATA_BUFFER;

and then simply say

itm->GenericReparseBuffer.DataBuffer = db;

Of course, the address stored there only makes sense as long
as what 'db' points to still exists. But if you want to create
a copy of what 'db' points to then you need to first of all
know the number 'len' of bytes 'db' points to and then allocate
memory and copy what 'db' points to, a la

itm->GenericReparseBuffer.DataBuffer = malloc( len );
memcpy( itm->GenericReparseBuffer.DataBuffer, db, len );

In that case you're responsible for getting rid of the
memory when it isn't used anymore or at least when what-
ever 'itm' points to goes out of scope or becomes de-
allocated.
Regards, Jens
 
C

Chris Saunders

My thanks to Jens Thoms Toerring and Mark McIntyre for responding. Sorry I
took awhile but I've been very busy. Thanks again.

Regards
Chris Saunders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top