title: How to use libusb and libusb_get_device_descriptor()?
body: <p>I'm learning to use libusb v1.0.0 for the first time on Ubuntu 12.10. Here is some small test code I'm using to try and understand how to use this API:</p>
  
  <pre><code>#include &lt;libusb-1.0/libusb.h&gt;
  ...
  libusb_device **list;
  libusb_get_device_list(ctx, &amp;list); // Returns 11 USB devices which is correct.
  for (size_t idx = 0; list[idx] != NULL; idx ++)
  {
      libusb_device *dev = list[idx];
      libusb_device_descriptor desc = {0};
      int rc = libusb_get_device_descriptor(dev, &amp;desc);
  </code></pre>
  
  <p>At this point, rc == 0, meaning it should have completed successfully. Source: <a href="http://libusb.sourceforge.net/api-1.0/group__desc.html#ga5e9ab08d490a7704cf3a9b0439f16f00">documentation for *libusb_get_device_descriptor()*</a>.</p>
  
  <p>But the structure <code>desc</code> is always empty. None of the fields ever get set. If I change the last two lines above to this:</p>
  
  <pre><code>    libusb_device_descriptor desc = {1, 2, 3, 4, 5, 6, 7, 8, 9};
      int rc = libusb_get_device_descriptor(dev, &amp;desc);
  </code></pre>
  
  <p>...then when <code>libusb_get_device_descriptor()</code> returns, I see <code>desc</code> remains unchanged, confirming for me that I'm not getting what I expect from this API.</p>
  
  <p>I've also tried to run <code>a.out</code> as root just in case this requires elevated privileges.  Doing a Google search on <code>libusb_get_device_descriptor</code> hasn't gotten me anywhere.</p>
  
  <p>Relevant commands I ran to try this code:</p>
  
  <pre><code>sudo apt-get install libusb-1.0.0-dev
  g++ -ggdb test.cpp -lusb-1.0
  ./a.out 
  </code></pre>
  
  <hr>
  
  <p>Ah! Crazy user error! sharth's code helped me figure it out. Here is the code I was actually using -- see if you can spot the error:</p>
  
  <pre><code>std::cout &lt;&lt; "rc == " &lt;&lt; libusb_get_device_descriptor(dev, &amp;desc) &lt;&lt; std::endl
            &lt;&lt; "vendor == " &lt;&lt; desc.idVendor &lt;&lt; std::endl;
  </code></pre>
  
  <p>I guess the way the compiler evaluates this, it is free to evaluate <code>desc.idVendor</code> before the call to <code>libusb_get_device_descriptor()</code> has actually been made. My bad.</p>