I am developing a tool to dump data from variables. I need to dump the variable name, and also the values.
My solution: Store variable name as a string, and print th
If you need to do this for arbitrary variables then you'll probably need to use a debugger API that the compiler or platform provides (like DbgHelp on Windows).
On some embedded systems I worked on we've needed to be able to display on command the values of certain important variables that are known ahead of time, and to do that all that we need is a simple Name/pointer table:
typedef
struct vartab {
char const* name;
int * var;
} vartab;
vartab varTable[] = {
{ "foo", &foo },
{ "bar", &bar }
};
Then I just used a little routine that searches the table for the variable name in question, and dumps the name and the data pointed to by the pointer. If you need to dump data other than plain ints, you can extend the structure to also hold a printf-style formatter and change the pointer to be a void* and pass that junk to snprintf() or something to format the data.
Sometimes I'll also use a macro that helps build the table (possibly also declaring the variable). But to be honest, I think that that really just makes it more complex to understand (especially for someone new joining the project - they often have a small "WTF?" moment) and doesn't really simplify things much.