0

I have a structure

typedef struct 
{
   UINT32 a;
   Char b,
   struct xyz *c;
}A;

Is there a way to generate the following information from the struct?

index type    field size
1     UINT32   a     4 
2     char     b     1
3     pointer  c     8

I understand that C doesn't allow reflection and we can use X-Macros to get all of these information, but my structures are extremely complex and have thousands of fields to modify and edit to support X-Macros. I really appreciate if there is a way or support in C, that I am unaware of

Any shell script or python script that can generate these?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
vip
  • 53
  • 1
  • 6
  • There's no way to do that in C that I know of, but a script in python could be possible, you'd have to tell us how your source code files look like and which structs do you want information on (all of them?) – Dunno Mar 29 '17 at 20:38
  • Short of writing a file parser, there is no way to do this in C. an _[Awk](https://www.tutorialspoint.com/awk/awk_basic_examples.htm)_ script in Linux would do it pretty easily though. By the way, the second member in your struct should have a `;` line terminator instead of `,`. – ryyker Mar 29 '17 at 20:44
  • Too broad and asking about tools is off-topic. However, you could do the other way'round and generate the C code from a textual representation. – too honest for this site Mar 29 '17 at 21:02
  • See http://stackoverflow.com/q/7674255/2630032 – Stephan Lechner Mar 29 '17 at 21:02
  • Thanks guys. All of my source code structured are nested into 1 big structure. So thought of extracting the struct information by starting with a small struct containing basic types. – vip Mar 29 '17 at 22:26
  • 1
    You say 'all of my source code structures are nested into one big structure'. That's likely to make things hard, to be polite about it. Are you sure that's a good idea? And how big is the big structure? Does the big structure mostly contain pointers to other structures, or are the other structures directly used in the big structure (or is there some mixture)? – Jonathan Leffler Mar 29 '17 at 22:33
  • Hi Jonathan. Thanks,,,Structure is really really big the size of 32k. yes it do contain pointers to other structures and structure arrays. I do think it is not a great idea , but want to start with simple structures and eventually leverage it to the big structure. – vip Mar 30 '17 at 01:44
  • Hi Stephen. I tried to add in my .c file, but compiler failed with error"no suck directory". which gcc version supports the metaresc? is this package free ? – vip Mar 30 '17 at 02:51

0 Answers0