Pointer array and structure in C -
i lose guessing how come given output . can please explanation? also, short , quick resources try similar questions?
void main() { struct { char ch[10]; char *str; }; struct s1={"hyderabad","bangalore"}; printf("\n%c%c",s1.ch[0],*s1.str); printf("\n%s%s",s1.ch,s1.str); getch(); }
ans: hb, hyderabadbangalore
struct s1={"hyderabad","bangalore"};
assigns "hyderabad" ch
, "bangalore" str
.
printf("\n%c%c",s1.ch[0],*s1.str);
prints first character of strings. since ch
array, ch[0]
represents first character. since str
character pointer, points first character of string here. so, *s1.str
have value 'b'
printf("\n%s%s",s1.ch,s1.str);
prints characters of both strings. fundamentally, ch
equal &ch[0]
, address of first character in array. and, str pointer variable holds address of first character of string literal "bangalore".
Comments
Post a Comment