A problem with structures and pointers ...

Discussion in 'C Programming' started by Quantum, Aug 17, 2005.

  1. Quantum

    Quantum Guest

    I have the following Code I have a problem with.
    DataValues and RegNbr etc ... Never get written to.
    There's a problem about accessing the data structure member sdata.
    Can Any body Help ...

    Reply to

    A C programmer ...
    typedef struct
    {
    Uint32 lwDataSize;
    Uint16 BoxIdOpFrom; /*The owner of the op*/
    Uint16 BoxIdOpTo; /*The destinataire op*/
    Uint16 wOpCode;
    Uint8 *sData;
    }CommStructData;

    RetCode OperationSetRegister(CommStructData *pStructDataIn)
    {
    Uint8 *DataValues;
    Uint8 RegNbr;
    Uint8 RegValue;
    RetCode err=ERR_OK;

    DataValues = (Uint8 *) pStructDataIn->sData;
    RegNbr = *DataValues;
    DataValues++;
    RegValue = *DataValues;

    Rs232RFWriteReg(RegNbr, RegValue);
    err = SendRegister(RegNbr, RegValue);

    }
     
    Quantum, Aug 17, 2005
    #1
    1. Advertising

  2. Quantum wrote:
    > I have the following Code I have a problem with.
    > DataValues and RegNbr etc ... Never get written to.
    > There's a problem about accessing the data structure member sdata.
    > Can Any body Help ...
    >
    > Reply to
    >
    > A C programmer ...
    > typedef struct
    > {
    > Uint32 lwDataSize;
    > Uint16 BoxIdOpFrom; /*The owner of the op*/
    > Uint16 BoxIdOpTo; /*The destinataire op*/
    > Uint16 wOpCode;
    > Uint8 *sData;
    > }CommStructData;
    >
    > RetCode OperationSetRegister(CommStructData *pStructDataIn)
    > {
    > Uint8 *DataValues;
    > Uint8 RegNbr;
    > Uint8 RegValue;
    > RetCode err=ERR_OK;
    >
    > DataValues = (Uint8 *) pStructDataIn->sData;
    > RegNbr = *DataValues;
    > DataValues++;
    > RegValue = *DataValues;
    >
    > Rs232RFWriteReg(RegNbr, RegValue);
    > err = SendRegister(RegNbr, RegValue);
    >
    > }


    You don't give the full context. Where exactly is
    sData allocated? You are treating it as an array of
    at least two pointers to Uint8, which hold the "RegNbr" and
    "RegValue" respectively. Do you somewhere malloc this array,
    as in foo->sData = malloc(ARRAY_ELTS * sizeof *foo->sData)?

    I'd get rid of the DataValues and its increment, and say that
    RegNbr = pStructDataIn->sData[0];
    RegValue = pStructDataIn->sData[1];
    But whatever, that is a matter of taste.

    The (Uint8 *) cast also looks dodgy, why are you casting
    something to the type it already is?

    -David
     
    David Resnick, Aug 17, 2005
    #2
    1. Advertising

  3. Quantum

    Quantum Guest

    accessing the sData member like this did solve the problem ....

    RegNbr = pStructDataIn->sData[0];
    RegValue = pStructDataIn->sData[1];

    A C programmer ...
     
    Quantum, Aug 17, 2005
    #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. tweak
    Replies:
    14
    Views:
    2,819
    Eric Sosman
    Jun 11, 2004
  2. Alfonso Morra
    Replies:
    11
    Views:
    754
    Emmanuel Delahaye
    Sep 24, 2005
  3. Jens Thoms Toerring

    Re: Problem with function pointers and structures

    Jens Thoms Toerring, Jul 2, 2009, in forum: C Programming
    Replies:
    7
    Views:
    357
    Richard Bos
    Jul 4, 2009
  4. Eric Sosman

    Re: Problem with function pointers and structures

    Eric Sosman, Jul 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    292
    Eric Sosman
    Jul 4, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    737
Loading...

Share This Page