Print the structure fields and values in C

user3555115 picture user3555115 · Dec 23, 2016 · Viewed 86.2k times · Source

I am interested in printing the structure fields .

Typedef struct
{
   UINT32 thread_id;
   BOOL   is_valid;
}T_THREAD;

Is there a way in "C" language to print the contents of a structure, something like

ex: print (T_THREAD) and output should be like

Contents of a structure T_THREAD are 
  thread_id
  is_valid

Answer

Dellowar picture Dellowar · Dec 23, 2016

What you're looking for is reflection. Java and other virtual languages has reflection so you can print out the variable names and function names for any given class. Because the compiler builds these reflection functions automatically.

C does not have reflection. You must do everything manually.