2

How do I print all the fieldnames of a struct which also has sub structs in them comma separated?.

typedef struct logTimestamp_s{
    unsigned short lower;
    unsigned short tsLow;
    unsigned int tsHigh;
} logTimestamp;

typedef struct logHeader_s{
    unsigned short length;
    unsigned short code;
    logTimestamp ts;
} logHeader;

typedef struct __attribute__ ((__packed__)){
    logHeader   hdr;                //12
    unsigned int res1;              //16       unknown 4 bytes.
    unsigned char id;               //17        /* sub packet id */
    unsigned char ver;              //18        /* sub packet version */
    unsigned short size;            //20        /* sub packet size */
    unsigned int res2;              //24       unknown 4 bytes.
}log11AB;

For ex, I am looking for a way to obtain printout like :

"length, code, lower, tsLow, tsHigh, res1, id, ver, size, res2"

Thanks!

YouHaveaBigEgo
  • 220
  • 6
  • 13

3 Answers3

4

You can't do that in a simple C program, I'm afraid. The names of the fields aren't available at runtime - only the compiler sees those. If you don't need to do it at runtime, you can look into using something like libclang to parse your source and extract the information you want.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Ah. I see. Ok. Point taken. How about this. Is there a way to print all the values of different data types inside a struct then? Edit : Hmm. libclang is new to me. Let me read up. thanks! – YouHaveaBigEgo Jan 21 '15 at 00:43
  • No, the language doesn't have compile-time reflection either. Of course, you can add other things to your project, which might be able to parse the C code. – Deduplicator Jan 21 '15 at 00:44
  • Darn. ok. I will then manually have to `printf` each field and its value of different datatypes of a super massive super structure. Oh well eh :-/. I thought of some easy way might be out there, but it doesn't seem to be that easy. its okay. – YouHaveaBigEgo Jan 21 '15 at 00:51
  • 'The names of the fields aren't available at runtime': Still a macro or some compile time reflection could have done the job (as it turns out it is not possible either), so that's not an explanation in any way. – agemO Oct 08 '18 at 07:49
2

First, don't laugh at my suggestion, but I actually did it.

You only need to print the struct while debugging, and the debugger have all the information. Normally, the struct is presented as treeview: the root have no data, to view the member's data you expand the root. Since I'm too lazy to expand them all (and roll and roll to see them), I grab each member's value and summarize them (commas delimited) in one line as data for the root (very wide view.)

Of course you don't have the source code of your favorite debugger! (I wrote mine, very primitive, plugin for ollydbg)

qak
  • 21
  • 1
  • You're suggestion is quite ok. I actually ended up doing something similar. I usually don't like to use GUI editors such as CodeBlocks, or Visual Studio. I am a big fan of command line kindof person. Thanks though for replying nonetheless. – YouHaveaBigEgo Jan 23 '15 at 18:42
1

Another option is to use a preprocessor macro to create your struct, which provides printing/reflection possibilities. The following SO answer provides a proof-of-concept.

Community
  • 1
  • 1
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345