How is the following code evaluated in C? -
#include <stdio.h> int main() { char *p="hello"; char *p1; p1=p; while(*p1!='\0') ++*p1++; printf("%s\t%s",p,p1); } what should output be? how code evaluated? should output ifmmp or runtime error?
you invoking undefined behavior, modify string literal within main's body:
char *p = "hello"; char *p1; p1 = p; while (*p1 != '\0') ++*p1++; // undefined behavior the following statement:
++*p1++; is same as:
++(*(p1++)); it can written 2 statements:
++(*p1); // increase *p1 1 p1++; // increase p1 1 if want output ifmmp (i.e. every letter replaced next one), change:
char *p = "hello"; with:
char p[] = "hello";
Comments
Post a Comment