-2

Sometimes we meet "struct xxxx;" in C file, take libusb.h which is the header file of libusb.so as example, as shown below (struct libusb_device):

struct libusb_context;
struct libusb_device;
struct libusb_device_handle;

What does this mean? Is it only a declaration? But I can't find where struct libusb_device is defined.

The whole libusb.h file is here: libusb.h.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • You need to read a good C programming book. Then look into some reference about [struct](http://en.cppreference.com/w/c/language/struct). After having read it, you should refer to the C standard, e.g. to [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) – Basile Starynkevitch Mar 17 '18 at 06:42
  • Those three declarations all say "there exists a structure type with the given name". The contents of the structure aren't defined, but you can now have and use pointers to those types. They can be used in function prototypes, for example. They are often called 'opaque' types. The full details of the type may only be defined in one source file, or they may be in a separate private header not distributed to users of the library. – Jonathan Leffler Mar 17 '18 at 06:47
  • I'm voting to close this question as off-topic because this not a programming problem. OP just doesn't know C. A book is needed – 0___________ Mar 17 '18 at 08:15
  • 1
    @PeterJ_01: Numerous questions retained in Stack,Overflow ask about some aspect of C, meaning the asker does not know or understand something about C. They are on topic because each is “a practical, answerable problem that is unique to software development.” While you may find some bit of knowledge to be elementary now, there was a time when you did not know it, and it can be documented for the benefit of future Stack Overflow users. – Eric Postpischil Mar 17 '18 at 08:43
  • @JonathanLeffler Now I know Those three declarations are 'opaque' types, The user does not know how it is implemented and does not need to know. To achieve the purpose of encapsulation. Thank you for all the answers and I know I need read more book, thanks! – Miles Franco Mar 17 '18 at 08:53

1 Answers1

1

This is a declaration (in C++) or an incomplete type declaration (in C):

struct libusb_context;

This is a definition (in C++) or a [complete type] declaration (in C):

struct libusb_context { int my_device_id; };

Knowing those terms, you can find many explanations of the difference. For example, look here: What is the difference between a definition and a declaration?

Tom
  • 939
  • 5
  • 9
  • 1
    This is actually from C++ terminology. C terminology does not include the concept of struct type definition. In C both are struct type *declarations*. The former one declares an incomplete type. The latter declares a complete type. – AnT stands with Russia Mar 17 '18 at 07:01
  • The incomplete type in C should still function identically to a C++ declaration, though, correct? – Tom Mar 17 '18 at 07:04
  • Updated to add the other term. Thanks for the assist. – Tom Mar 17 '18 at 07:11