AnsweredAssumed Answered

really just another question on pointer scope

Question asked by Ryan Lush on Jan 8, 2018
Latest reply on Jan 10, 2018 by Daniel Chen

I wrote this code to demonstrate something I found in the KSDK USB library. I took (interpreted?) this code from usb_device_ch9.c starting around line 862.

 

typedef struct _usb_device_control_request_struct
{
    usb_setup_struct_t *setup; /*!< The pointer of the setup packet data. */
    uint8_t *buffer;           /*!< Pass the buffer address. */
    uint32_t length;           /*!< Pass the buffer length or requested length. */
    uint8_t isSetup;           /*!< Indicates whether a setup packet is received. */
} usb_device_control_request_struct_t;

 

void main()
{
    function1();
}

 

void function1()
{
    uint8_t *buffer = (uint8_t *)NULL;
    uint32_t length = 0U;
    usb_device_control_request_struct_t controlRequest;
    
    controlRequest.buffer = (uint8_t *)NULL
    
    function2(&controlRequest);
    
    length = controlRequest.length;
    buffer = controlRequest.buffer;

 

}

 


void function2(void* param)
{
    usb_device_control_request_struct_t* controlRequest = (usb_device_control_request_struct_t*)param;

 

    function3(controlRequest)
}

 

uint8_t buffer[8]
void function3(usb_device_control_request_struct_t* controlRequest)
{
    controlRequest.buffer = buffer;
    controlRequest.length = 8;
}

 

The expectation is function2 will give controlRequest.buffer something to point to but controlRequest.buffer is still null when in the context of function1() just after calling function2(). I would never do it this way but this is the framework of the KSDK USB driver. I'm probably forgetting to explain something here but hopefully I can at least get a conversation going?

Outcomes