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 like print_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!
C/C++ does not support to print out struct's field name. This is a guide showing how to print struct field and value using macros.
In C, we can get the memory address of any variable or member field (of struct). To do so, we use the address of (&) operator, the %p specifier to print it and a casting of (void*) on the address.
A struct in the C programming language (and many derivatives) is a composite data type (or record) declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the ...
Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).
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'); };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With