5

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.

nalzok
  • 14,965
  • 21
  • 72
  • 139

6 Answers6

11

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Typically, `FILE` is defined as `typedef struct _iobuf FILE` for historic reasons. – fuz Apr 08 '16 at 15:30
2

The simple answer is no. You have to have the source code.

Logicrat
  • 4,438
  • 16
  • 22
2

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.

Byron Jones
  • 315
  • 2
  • 11
1

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.

abhiarora
  • 9,743
  • 5
  • 32
  • 57
0

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.

Rudi
  • 95
  • 9
-1

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.

edmz
  • 8,220
  • 2
  • 26
  • 45
  • @OrangeDog No, I mean parsing it from code. It need not be C. – edmz Apr 08 '16 at 15:22
  • OK, that's a terrible idea. – OrangeDog Apr 08 '16 at 15:22
  • @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