arrays - Pointer and String Outputs in C -
i trying understand why following piece of code produces output of bcd123 123.
void f(char *p) { *p += 1; } int main() { int i; char a[] = "abc" "123"; char *p = a; (i = 0; < 3; i++) f(p++); printf("%s ", a); printf("%s ", p); return 0; }
void f(char *p) { *p += 1; }
adds 1 character passed in. 'a' + 1 = 'b' etc. in ascii table http://www.asciitable.com/
part 2
char a[] = "abc" "123"; // same char a[] = "abc123"; (i = 0; < 3; i++) f(p++); <<<<===== moves p along string 3 places (once each loop) printf("%s ", a); printf("%s ", p); <<< p points @ 4th char
Comments
Post a Comment