How to "study" the structure?

Take the type FILEdefined in stdio.h, for example: is there a way to get information about your fields (name, size, offset, etc.) without looking at the header? Sometimes it’s convenient to have such a function / macro to check the structure components.

+4
source share
6 answers

No.

There is no metadata associated with data structures in C; all this is lost during compilation.

And this is completely possible, since it is FILEopaque that there is no common title, in fact, has a definition. It could just be typedef struct __FILE FILE;in the library header, and then all the details should be stored inside, perhaps in code in which you don't even have a source.

+10

- . .

+1

C , "" - . , , .

+1

- .

, , Header File comments, .

/ , , C.

0

- api/software, , .

0
source

It's impossible. C does not save such information, probably because of the principle "you do not pay for what you do not use"; It also keeps the language simple and portable.

What you can do the most is query the position of a given member in struct, using a standard macro offsetof. But you must know the name of the field.

-1
source

All Articles