really just another question on pointer scope

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

really just another question on pointer scope

562 Views
ryanlush
Contributor IV

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?

0 Kudos
3 Replies

451 Views
bobpaddock
Senior Contributor III

Isn't function3 wrong, among other issues?

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

should be:

{

   controlRequest->buffer = buffer;

...

}

0 Kudos

451 Views
danielchen
NXP TechSupport
NXP TechSupport

Yes, controlRequest is a pointer in function3, should be  controlRequest->buffer = buffer;  or else there will be a compile error.

Regards

Daniel

0 Kudos

451 Views
danielchen
NXP TechSupport
NXP TechSupport

Hi Ryan Lush

I am not sure I fully understand you, I copied your code to hello world project, and found the controlRequest.buffer point to 0x2002FFE0.  Please see below picture.

pastedImage_1.png


Have a great day,
Daniel

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos