I have a structure and want to pass it as an argument to printf
by casting it to (char*)
. But I get gibberish output. I have learned that the pointer to the structure is also an pointer to its first member so I thought this typecast should work.
Why it doen't?
#include <stdio.h>
#include <string.h>
typedef struct str {
char *s;
} str;
int main(void)
{
str s;
s.s = "Hello World!";
printf("%s\n", (char *) &s);
}
Output:
(random bytes)