Thread: Puffers, pointers and such

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    Smile Buffers, pointers and such

    Hi

    I am trying to cast a byte buffer (that is contained within a struct) to a different struct variable:
    Code:
    typedef struct {
    	
    	uint8_t protocolVersion;
    	uint8_t messageCount;
    	uint8_t payload[9];
    
    } PACK Data_t;
    
    typedef struct {
    	
    	uint8_t EE_Header;
    	uint16_t DstShortAddress;
    	uint16_t FV_Version;
    	uint16_t ProtocolVersion;
    	uint8_t EE_Start;
    	uint8_t EE_End;
    
    } PACK PowerData_t;
    
    Data_t iData;
    
    PowerData_t* LPD = (PowerData_t*)iData->payload;
    The structs are being used in a comms system to allow the passing of structured packets back and forth between devices. The payload in the iData structure needs to be casted to the PowerData type.

    Now the above works fine and dandy, however after I have the local variable LPD with the correct data how do I save this data to a global variable for use after the function doing the converting (above) has finished and the memory for LPD has been re-allocated?

    Code:
    GlobalPowerData = *LPD; // does work but not as I need:
    What I am getting at the moment is that the data comes in fine, however by the time I come to use the global data variable the contents has been over written or altered as the pointer to the local variable (LPD) created no longer apply.

    Any help anyone can give would be greatly appreciated

    David
    Last edited by DavidDobson; 03-19-2009 at 03:53 AM. Reason: My terrible spelling

Popular pages Recent additions subscribe to a feed