+ 1

Need help with pointers

#include <stdio.h> int main() { char *s= "hello"; char *p = s; printf("%c\t%c", p[0], s[1]); } when executed output was : h e why??? in printf.. i typed p[0] not s[0]... still it prints h need some explanation here..

12th Mar 2018, 3:13 PM
Ayush walia
Ayush walia - avatar
1 Resposta
+ 3
p is of the same type as s. after the assignments p and s are both of the same value (ie the address of the first character of the string "hello") So p[0] is the same as s[0], which is h.
12th Mar 2018, 3:41 PM
ifl
ifl - avatar