Is trying to access an uninitialized struct field in C considered undefined behavior?
struct s { int i; };
struct s a;
printf("%d", a.i);
Is trying to access an uninitialized struct field in C considered undefined behavior?
struct s { int i; };
struct s a;
printf("%d", a.i);
depending on the storage duration of the variable:
struct
{
int a;
int b;
}c;
int main()
{
struct
{
int a;
int b;
}e;
static struct
{
int a;
int b;
}s;
printf("%d", c.a); // <- correct no UB
printf("%d", s.a); // <- correct no UB
printf("%d", e.a); // <- UB
}
structure c
& s
have a static
storage duration and they are always initialized. If programmer does not initialize them explicit way they are zeroed.
structure e
has the automatic storage duration and it is not zeroed if not explicitly initialized by the programmer