Print a struct in C
Solution 1
You can always do a hex dump of the structure:
#define PRINT_OPAQUE_STRUCT(p) print_mem((p), sizeof(*(p)))
void print_mem(void const *vp, size_t n)
{
unsigned char const *p = vp;
for (size_t i=0; i<n; i++)
printf("%02x\n", p[i]);
putchar('\n');
};
Solution 2
There is nothing like RTTI in C, only solution (apart from hex dump like above) is to #define dump function together with other #defines, ie.
#if _DEBUG
struct { ..... }
#define STRUCT_DUMP(x) printf(.....)
#else
struct { ..... } // other version
#define STRUCT_DUMP(x) printf(.....) // other version dump
#endif
Related videos on Youtube
Albus Dumbledore
For most of my time I do programming stuff, but I like math, too, especially if it’s got a more applied nature. I love jazz music and action-packed thrilling books, where the good guys are noble and able, but sound self-deprecating, and always think coolly and clearly. Most of all, however, I like video games with compelling atmosphere, innovative design and great eye for detail. I am best at Java, but I also have experience with C++, Python, Ruby, Visual Basic, Pascal, ActionScript and PHP. I have some idea of functional programming, too, as I’ve done some good amount of projects in Matlab and Mathematica. I prefer simpler code, but I am not too scared to go deep, if it’s the only option. My love for books and mobile devices has leaded me to making my own ebook reader: The AlbiteREADER. One can find free ebooks there, too. It’s a big thing for me, for I’ve been making the app for over four months. As far as math is concerned, I don’t like it raw, but prefer it in connection with other sciences, i.e. numerical analysis, discreet math, statistics, biomathematics, etc. I’ve done some good amount of math projects with Matlab and Mathematica. I’ve also had the chance to teach biomath as an assistant, i.e. I was responsible for the demonstrational part of the subject. In relation with that, I can say, I wrote some good quantity of Mathematica code and some lesser amount of mathematical stuff.
Updated on July 09, 2022Comments
-
Albus Dumbledore almost 2 years
I am trying to print a
struct
that is coming as an argument in a function in order to do some debugging.Is there anyway I could print a structure's contents without knowing what it looks like, i.e. without printing each field explicitly? You see, depending on loads of different
#define
s the structure may look very differently, i.e. may have or not have different fields, so I'd like to find an easy way to do something likeprint_structure(my_structure)
.NetBeans' debugger can do that for me, but unfortunately the code is running on a device I can't run a debugger on.
Any ideas? I suppose it's not possible, but at least there may be some macro to do that at compilation time or something?
Thanks!
-
Jonathan Fuerth over 4 yearsThis is now possible in the clang compiler. See my response to a similar question. (Not sure if SO etiquette is to post a duplicate answer here, or link to the other one)
-
-
Lundin over 13 yearsThis code will also display the contents of the padding bytes. They may contain any value and may be located anywhere inside the struct.
-
Fred Foo over 13 years@Lundin: that's true. But with some knowledge of the compiler, this may still contain useful information. You'd have to know where to look.
-
kuafu about 11 yearswhen I compile this function,it tells print_struct.cpp: In function ‘void print_mem(const void*, size_t)’: print_struct.cpp:14:30: error: invalid conversion from ‘const void*’ to ‘const unsigned char*’ [-fpermissive]
-
Fred Foo about 11 years@young001: that's because it's written in C. In C++, you need an explicit cast to
unsigned char const *
. -
sehari24jam almost 9 yearsslight better, with grouping and line wrapping:
printf("%02x%s", p[i], i==0 ? "" : i % 16 == 15 ? "\n" : i % 4 == 3 ? " " : "");
-
3Dave over 7 yearsI strongly disagree with the notion that cramming multiple ternary operators into a parameter is "better."