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

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have been pursuing this, trying through experimentation to find… May 16, 2026 at 1:57 pm
  • Editorial Team
    Editorial Team added an answer you should add drag handler, dispatch new click event for… May 16, 2026 at 1:57 pm
  • Editorial Team
    Editorial Team added an answer I think it should rather be something like that: <img… May 16, 2026 at 1:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

This code compiles and works as expected (it throws at runtime, but never mind):
I'm attempting to help our network engineers troubleshoot a situation for one of our
I'm attempting to use Mono and Monodevelop (the IDE) to code a C# project
I am writing JVMTI code to profile Java programs, which mostly entails obtaining stack
I am trying to grab the data from the database and display all the
I'm trying to learn how to best use OOP in PHP. Please be aware
i have a c problem can any one help me . i wrote a
Thanks for your attention and time. I'm modifying an existing JavaScript but can't understand
I have tried a lot to learn linked list.But all my efforts were wasted.Please
I need to make a web application and I want to use MVC. However,

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.