Assigning a pointer to an array

Discussion in 'C Programming' started by Chris Saunders, Jun 7, 2008.

  1. 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
     
    Chris Saunders, Jun 7, 2008
    #1
    1. Advertising

  2. Chris Saunders <> wrote:
    > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jun 7, 2008
    #2
    1. Advertising

  3. 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
     
    Chris Saunders, Jun 9, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The Hole Pounder

    Assigning a pointer to an array

    The Hole Pounder, Sep 11, 2010, in forum: C Programming
    Replies:
    10
    Views:
    631
    Shao Miller
    Sep 13, 2010
  2. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    494
    James Kuyper
    Sep 23, 2011
  3. Richard Lionheart
    Replies:
    27
    Views:
    439
    Jean-Hugues ROBERT
    May 4, 2004
  4. weston
    Replies:
    1
    Views:
    278
    Richard Cornford
    Sep 22, 2006
  5. bintom
    Replies:
    11
    Views:
    675
    Luca Risolia
    Oct 15, 2012
Loading...

Share This Page