Assigning a pointer to an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Saunders

    Assigning a pointer to an array

    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_D ATA_BUFFER
    {
    DWORD ReparseTag;
    WORD ReparseDataLeng th;
    WORD Reserved;
    GUID ReparseGuid;
    struct {BYTE DataBuffer[1]; } GenericReparseB uffer;
    } REPARSE_GUID_DA TA_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_r eparse_buffer_d ata_buffer_poin ter (a_item, v: POINTER) is
    external
    "C inline use <Windows.h>"
    alias
    "[
    {
    PREPARSE_GUID_D ATA_BUFFER itm;
    BYTE* db;
    itm = $a_item;
    db = $v;
    &((itm->GenericReparse Buffer).DataBuf fer) = db;
    }
    ]"
    end

    Line 5 of the C code part is where I attempt to assign the array
    "DataBuffer[]" in the struct "GenericReparse Buffer" which is a member of the
    struct "REPARSE_GUID_D ATA_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

  • Jens Thoms Toerring

    #2
    Re: Assigning a pointer to an array

    Chris Saunders <evas@mountainc able.netwrote:
    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_D ATA_BUFFER
    {
    DWORD ReparseTag;
    WORD ReparseDataLeng th;
    WORD Reserved;
    GUID ReparseGuid;
    struct {BYTE DataBuffer[1]; } GenericReparseB uffer;
    } REPARSE_GUID_DA TA_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_r eparse_buffer_d ata_buffer_poin ter (a_item, v: POINTER) is
    external
    "C inline use <Windows.h>"
    alias
    "[
    {
    PREPARSE_GUID_D ATA_BUFFER itm;
    BYTE* db;
    itm = $a_item;
    db = $v;
    &((itm->GenericReparse Buffer).DataBuf fer) = db;
    }
    ]"
    end
    Line 5 of the C code part is where I attempt to assign the array
    "DataBuffer[]" in the struct "GenericReparse Buffer" which is a member of the
    struct "REPARSE_GUID_D ATA_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->GenericReparse Buffer).DataBuf fer) = 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->GenericReparse Buffer.DataBuff er[ 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_D ATA_BUFFER
    {
    DWORD ReparseTag;
    WORD ReparseDataLeng th;
    WORD Reserved;
    GUID ReparseGuid;
    struct { BYTE *DataBuffer; } GenericReparseB uffer;
    } REPARSE_GUID_DA TA_BUFFER, *PREPARSE_GUID_ DATA_BUFFER;

    and then simply say

    itm->GenericReparse Buffer.DataBuff er = 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->GenericReparse Buffer.DataBuff er = malloc( len );
    memcpy( itm->GenericReparse Buffer.DataBuff er, 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 ___ jt@toerring.de
    \______________ ____________ http://toerring.de

    Comment

    • Mark McIntyre

      #3
      Re: Assigning a pointer to an array

      Chris Saunders wrote:
      struct {BYTE DataBuffer[1]; } GenericReparseB uffer;
      DataBuffer is an array of size 1.
      &((itm->GenericReparse Buffer).DataBuf fer) = db;
      And here you try to assign to the array. You can't do that. If you want
      DataBuffer to be assignable, make it a pointer.
      struct {BYTE *DataBuffer; } GenericReparseB uffer;
      Line 5 of the C code part is where I attempt to assign the array
      "DataBuffer[]" in the struct "GenericReparse Buffer" which is a member of
      the struct "REPARSE_GUID_D ATA_BUFFER". My Eiffel compiler translates
      the Eiffel code into C and uses MSVC++ to compile it.
      Your Eiffel compiler seems to be defective!

      Comment

      • Chris Saunders

        #4
        Re: Assigning a pointer to an array

        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

        Comment

        Working...