c - Find the fault or error -
can find wrong following code?
int main(){ char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); }
i expected print space followed string!
here way use ++*p++
:
#include <stdio.h> #include <string.h> int main(int argc, char* argv[]) { char *p = "my test string", p1[15]; strncpy(p1, p, 14); p1[14] = 0; p = p1; while (*p != 0) { printf("%c", ++*p++); } }
note p1
array of memory (usually) allocated on stack, whereas p
(usually) pointer read memory, above code moves point p1
instead of (usually) read location string resides. operating systems and/or compilers exhibit other behavior, 1 of protection mechanisms built modern oss prevent classes of viruses.
Comments
Post a Comment