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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -