Take the FILE
type defined in stdio.h
for example: Is there any way to get the information about its fields(name, size, offset, etc) without taking a look at the header? Sometimes it'll be convenient to have such a function/macro to check the components of a struct.

- 14,965
- 21
- 72
- 139
-
3No, such metainformation is not available from a C program. – Jabberwocky Apr 08 '16 at 15:10
-
Maybe you should ask a new question regarding the situation where you think it would be useful - you may discover a better way to do things. – OrangeDog Apr 08 '16 at 15:23
-
1If compiled with debugging information and a format supporting it, look into the object file. But that looks like an XY-problem. – too honest for this site Apr 08 '16 at 15:24
-
[XY-problem](http://meta.stackexchange.com/a/66378/155659) – OrangeDog Apr 08 '16 at 15:30
-
`FILE` is opaque and implementation dependent - don't use it's fields. – Myst Apr 08 '16 at 17:10
-
To get a data dump of any object with a known size, could use [this](http://stackoverflow.com/a/35367414/2410359). No field nor meta info available, just the bits/bytes are reported. – chux - Reinstate Monica Apr 08 '16 at 17:31
6 Answers
No.
There's no meta data associated with data structures in C, all of that is lost when compiling.
And it's perfectly possible, since FILE
is opaque, that no public header actually has the definition. It could just be typedef struct __FILE FILE;
in the library header, and then all the details be kept on the inside, possibly in code you don't even have the source to.

- 391,730
- 64
- 469
- 606
-
Typically, `FILE` is defined as `typedef struct _iobuf FILE` for historic reasons. – fuz Apr 08 '16 at 15:30
In a C based structure, the data is stored in a way that is not "self defining" - you must know the structure definition to interpret the data. This reduces the size of the data to its bare minimum, and makes access faster, provided that your program understands the structure.

- 315
- 2
- 11
The answer is no.
Whenever, i needed to find information about a struct's data member, Header File
and comments
over there were sufficient for me.
And you can't have a function/macro to check the components of a struct because there is no meta data associated with the variables and procedures in C.

- 9,743
- 5
- 32
- 57
The only way to see the information about it's fields would be to have the actual source, or an api/software document that explains how the struct is organized.

- 95
- 9
It is not possible. C does not retain this kind of information, likely due to the "you don't pay for what you don't use" principle; it also keeps the language simple and portable.
What you can do at most is querying the position of a given member in the struct
, through the offsetof
standard macro. But you have to know the name of the field.

- 8,220
- 2
- 26
- 45
-
-
-
@OrangeDog Motivation? I may understand its complexity, but it's not a real argument. And it has nothing to do with the question itself, in any case. – edmz Apr 08 '16 at 15:25
-
In response to a C question about structs, you have recommended writing code to parse the header file. There are enough dangers in C as it is, without trying to make a program that compiles itself in order to change its behaviour at runtime. – OrangeDog Apr 08 '16 at 15:28
-
@OrangeDog No, I have not: I've just given a _possible_ solution. There're many others. I'll give you that I shouldn't have as it's beyond the point of the question and I thus have edited accordingly. But the rest of the answer is still fine. – edmz Apr 08 '16 at 15:34
-
Now "your best option is" has been removed, I have reversed my downvote. – OrangeDog Apr 08 '16 at 15:36