c - Union nested in a structure returning garbage value -
c - Union nested in a structure returning garbage value -
everything working fine, except nested union not getting updated. using mingw compiler. learning coding regarding c , trying out nested union , other stuff when problem came up. please tell me error of code, along possible debugging. couldn't find wrong it.
output:
a:3
b:3
l.a: 8
l.b: 5
union: -536870912
#include<stdio.h> #include<conio.h> typedef struct { int a; int b; } two; typedef union { int c; float d; } ad; typedef struct { int a; int b; 2 l; advertisement n; /*this nested union not getting updated*/ } one; void trr(one *p); int main() { 1 tr={2,3,{4,5},{.d=5.43}}; trr(&tr); printf("a: %d\nb: %d\nl.a: %d\nl.b: %d\nunion: %d",tr.a,tr.b,tr.l.a,tr.l.b,tr.n.d); homecoming 0; } void trr(one *p) { p->a=(*p).a+1; p->l.a=p->l.a*2; }
tr.n.d
has type float
. %d
format specifier tells printf
treat int
however. seek changing format specifier %f
instead
printf("a: %d\nb: %d\nl.a: %d\nl.b: %d\nunion: %f" // ^
alternatively, initialise int
fellow member of union:
one tr={2,3,{4,5},{.c=42}};
c
Comments
Post a Comment