2

Also tested with -Wall and -std=c99 and c11 (not working of course)

$ gcc -o usb -L/usr/local/lib -lusb-1.0 -I/usr/local/include usbtest.c

Output:

usbtest.c: In function ‘main’:
usbtest.c:14:1: error: label ‘brd_ftdi’ used but not defined
 libusb_get_device_list(context, &&brd_ftdi);

usbtest.c:

#include <stdio.h>
#include <libusb-1.0/libusb.h>
int main() {
  libusb_device* brd_ftdi;
  libusb_device_handle** brd_ftdi_handle;
  libusb_context* context;

  //libusb_device *** list = &&brd_ftdi;

  //libusb_init (libusb_context **);
  libusb_init (&context);

  libusb_get_device_list(context, &&brd_ftdi);
  //libusb_open(brd_ftdi, brd_ftdi_handle);

  //libusb_exit(struct libusb_context *); 
  libusb_exit(context);

  return 0;
}

Not defined? I know, but libusb_get_device_list() is supposed to do it (put the list in there), so WTF? I also tested declaring a pointer to pointer to brd_ftdi and passing that on instead but same result.

Leiaz
  • 2,867
  • 2
  • 20
  • 23

1 Answers1

2

If you read closely the error message, it says the label brd_ftdi is not defined, not the variable.

That is because you wrote &&brd_ftdi, the && is a GCC extension that means the address of a label. It is not standard C.

The address of a variable is just one &. The address of the brd_ftdi variable is &brd_ftdi.

The libusb_get_device_list function takes a libusb_device*** : the address where it'll write the address of the list of pointers to libusb_device. You can use it like this :

libusb_device **list_of_devices;
libusb_get_device_list(context, &list_of_devices);

list_of_devices is a pointer to the first pointer of the list (of pointers to libusb_devices).

You'll have a problem with your currently commented call to libusb_open, because you are passing an uninitialized pointer to a pointer to libusb_device_handle. This function too writes its result (the libusb_device_handle *) at the address you give.

Leiaz
  • 2,867
  • 2
  • 20
  • 23