I start learning C. It is fun. But I came to the point when I was trying to play around with struct
. Below you have my program which I have created and I notice that behaviour of struct
is different depend on how it was created.
When I run the program below my output is surprising. Output:
1. 1 0.000000 (some strange chars)
2. 0 0.000000 (null)
First of all like you can see the output of 1, 2 is different even if it is related to the same struct
.
Second of all number 3 is not printed at all.
I wonder why is that? I found on stackoverflow some explanation about the c struct
but non of this post touch that issue.
I was wonder if that is because of compiler? Or this is normal feature of C which I have to just understand? Or do I have even think about it?
#include <stdio.h>
struct s1 {
int int1;
double double1;
char *string;
} strc;
typedef struct {
int int1;
double double1;
char *string;
} DStruct;
int main() {
struct s1 defqu;
DStruct defqu2;
printf("1. %d %f %s\n", defqu.int1, defqu.double1, defqu.string);
printf("2. %d %f %s\n", strc.int1, strc.double1, strc.string);
printf("3. %d %f %s\n", defqu2.int1, defqu2.double1, defqu2.string);
return 0;
}