Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 985225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:08:02+00:00 2026-05-16T05:08:02+00:00

Please help me understand 2 things I found in this C code: First, there

  • 0

Please help me understand 2 things I found in this C code:

First, there is the whole code:

usbMsgLen_t usbFunctionSetup(uchar data[8])  
{  
usbRequest_t    *rq = (void *)data;  
static uchar    dataBuffer[4];  /* buffer must stay valid when usbFunctionSetup returns */  
if(rq->bRequest == CUSTOM_RQ_ECHO){ /* echo -- used for reliability tests */  
        dataBuffer[0] = rq->wValue.bytes[0];  
        dataBuffer[1] = rq->wValue.bytes[1];  
        dataBuffer[2] = rq->wIndex.bytes[0];  
        dataBuffer[3] = rq->wIndex.bytes[1];  
        usbMsgPtr = dataBuffer;         /* tell the driver which data to return */  
        return 4;  
    }else if(rq->bRequest == CUSTOM_RQ_SET_STATUS){  
        if(rq->wValue.bytes[0] & 1){    /* set LED */  
            LED_PORT_OUTPUT |= _BV(LED_BIT);  
        }else{                          /* clear LED */  
            LED_PORT_OUTPUT &= ~_BV(LED_BIT);  
        }  
    }else if(rq->bRequest == CUSTOM_RQ_GET_STATUS)  {  
        dataBuffer[0] = ((LED_PORT_OUTPUT & _BV(LED_BIT)) != 0);  
        usbMsgPtr = dataBuffer;         /* tell the driver which data to return */  
        return 1;                       /* tell the driver to send 1 byte */  
    }  
    return 0;   /* default for not implemented requests: return no data back to host */  
}  

Now, usbFunctionSetup gets array of 8 unsigned chars. Now there comes the line:

usbRequest_t    *rq = (void *)data;

So, I get the left side of the statement, but what is on the right? I know that (void *) is cast to this type, but why?

And second question is, isnt this code inefficient? Because first function receives 8 bytes of data, and than it creates additional pointer to them. And that additional pointer is created, at least if I am right, just to be able to access individual data by its name defined in usbRequest_t struct. Wouldn’t be simpler and more efficient to just use in code instead of rq->bRequest == something just for example
data[2]==something or if bRequest is bigger than one byte, for example data[1] == low_byte_of_something && data[2]== high_byte_of_something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:08:03+00:00Added an answer on May 16, 2026 at 5:08 am

    The function is getting a ‘raw’ buffer of data, and the line:

    usbRequest_t    *rq = (void *)data;
    

    is just creating a pointer to that buffer to access it using the data layout specified by the usbRequest_t struct.

    There’s absolutely nothing expensive about this operation – the compiler will likely not even actually create a new pointer variable in an optimized build.

    On there other hand, there may well be portability concerns, but that might not be important for your particular application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me understand the following code snippet :- def any(l): whether any number
Please help me understand this recursive function... var stack = Array; function power(base, exponent){
I'm a noob; please help me understand this authentication config / bindings stuff that
the following code is not behaving like I would expect. Please help me understand
Can anyone please help me to understand how should i access json data in
Please help me. I can't understand why this function, that uses texture memory __global__
Please help me with this update query. I COMPLETELY understand how redundant this is
Please help me understand why add1() and add4() report errors and why add2() and
Please help me understand where I am going wrong so much so that I
Please could someone help me understand why the div.fl element shown in Developer Tools

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.