C empty struct — what does this mean/do?

后端 未结 5 728
予麋鹿
予麋鹿 2020-12-25 11:04

I found this code in a header file for a device that I need to use, and although I\'ve been doing C for years, I\'ve never run into this:

struct device {
};
         


        
5条回答
  •  执念已碎
    2020-12-25 11:29

    One reason might to do this for a library is that the library developers do not want you to know or interfere with the internals of these struct. It these cases they may provide an "interface" version of the structs spi_device/device (which is what you may see) and have a second type definition that defines another version of said structs for use inside the library with the actual members.

    Since you cannot access struct members or even create compatible structs of that type yourself with that approach (since even your compiler would not know the size actual size of this struct), this only works if the library itself creates the structs, only ever passes you pointers to it, and does not need you to modify any members.

提交回复
热议问题