Is it possible to display a struct in stdout in one go? Letīs say you got a struct with different data types, it would be convenient to be able to print to stdout reffering only to the struct, not the individual members.
Printable View
Is it possible to display a struct in stdout in one go? Letīs say you got a struct with different data types, it would be convenient to be able to print to stdout reffering only to the struct, not the individual members.
Short answer is no.
If you're using gcc with the GNU LibC, then you can hook in your own printf format styles, but you might be pushing your luck with structs.
Thanks Salem. I am using gcc but I think Iīm going to take no for an answer, for now at least.
You can at least print struct as chars. Do a print function which takes structs size ptr to struct as void ptr.
Then just print the values in structs as chars in hexa format (in loop). When you know what struct you printed, you can quite easily calculate the values of independent members later. Just remember that if you do not have used any packing pragmas, compilers often add padding to keep default (usually 32 bit) alignation (or how do you say that).
Maz, thatīs an interesting approach. Iīll look into it.
Cheers.
That is not a very good solution... it gives pretty much some "non-sensical" output since all the data would be printed as hexa-decimal and it's not immediately obvious (perhaps ever?) what they are.
I suppose not, but you could write a custom function to output the data. Then all it would take is a single line to call the function.
No doubt I'm stating the obvious, but you can always wrap your struct-displaying code into one function.
In C++, you can overload the << operator for your data type, but of course this isn't possible in C.Code:struct person {
char *age;
int name;
};
void print_person(struct person who) {
printf("%s, age %d", who->name, who->age);
}
struct person joe;
joe.name = malloc(10);
strcpy(joe.name, "Joe");
joe.age = 33;
print_person(joe);
printf("\n");
I have been trying that, but never successfully :-D. I was not sure how I should interpret that it was not possible, started to fear that making a print function was out of the question.
This is what I have come up with so far.
main:
printStruct:Code:typedef struct {
int foo;
char name[25];
} Test;
Test myTest;
int *pt = NULL;
myTest.foo = 100;
myTest.name[0] = 'F';
myTest.name[1] = 'r';
myTest.name[2] = 'e';
myTest.name[3] = 'd';
printStruct(&myTest.foo);
This outputs the int correctly but only the last letter of name.Code:int printStruct(int *first) {
int foo;
char name[25];
foo = *first;
printf("Foo: %d\n", foo);
first++;
name[0] = (char)*first;
printf("Name: %s\n", name);
return 0;
}
dwks,
Some of that code looks a bit forreign to me still, what is the -> operator doing?
Thanks guys!
pTest->foo is the same as (*pTest).foo.Code:typedef struct
{
int foo;
char name[25];
} Test;
int main()
{
Test myTest;
myTest.foo = 100;
strcpy(myTest.name, "Fred");
printStruct(&myTest);
}
void printStruct(Test* pTest)
{
printf("Foo: %d\n", pTest->foo);
printf("Name: %s\n", pTest->name);
}
You were making this much harder than it needs be. I'm guessing you haven't learned all that much about C, yet.
So you suggest writing own print function for each struct?
And when you print the memory occupied by struct - char by char in hexa format, it is pretty easy to see what's stored in a struct (as long as we remember endianess issues and padding). I wonder where have you got the impression reading hexa is hard???
Another option is to use #ifdefs and and create debug structs which contain the size of struct as first member, and then type of members in struct. That way one could print whole struct's contents in more easily read format, and disable these prints after debug stage is over - with only one function, no matter how many different structs there is.
I never could restore the float value looking at its hex representation...Quote:
I wonder where have you got the impression reading hexa is hard???
Interesting. Can you give an example to illustrate your point?Quote:
And when you print the memory occupied by struct - char by char in hexa format, it is pretty easy to see what's stored in a struct (as long as we remember endianess issues and padding).
Allright peeps. I did not mean to start a war (here too) ;)
And I admitt I forgot the floats and doubles, they're harder. It has been long since I last needed floats or doubles. But I am positive that with a decent amount of searching, you can find out how they're represented, and when you convert hexas to binary, take your calculator and spend some time more, you'll eventually have those solved too :D
simple non tested, straight to post coded example:
example:Code:void print_struct( void *struct, size_t structSize)
{
size_t i;
char *printme=(char *)struct;
int formatter=1;
for( i=0;i < structSize;i++ )
{
printf("%02x ",*printme++):
if(!(formatter%4))
{
printf("\n");
}
}
}
typedef struct foo
{
int first;
char second;
char third;
char fourth;
void *fifth;
}foo;
print might produce:
A1 00 00 00
4D 61 7A 00
00 00 00 00
Now we can analyze this,
Knowing I am using PC in 32 bit arch, I know addresses are 32 bit wide, and it uses little endian arch. So let's see.
first 4 bytes (first row) is the int's value. Little endian system, so least significant bytes are first, EG int is A1 in hexa, which is 161 in ints. Then tehre's 3 chars, Eg chars 4D 61 7A. Quick look at ASCII table says, it is Maz.
The last 00 in that row is pretty likely to be the padding, to maintain default 32 bit alignation.
Last is void pointer, and as we see it is pointing at NULL address.
What was the hard part?