What is the issue with this reverse array code in c++? -
the program compile fine, crashes when running , shows : process terminated status -1073741819
void reverse(char *str){ char * end1 = str; char tmp = 'c'; if(str){ while(*end1){ ++end1; } --end1; while(str<end1){ tmp=*str; *str=*end1; *end1=tmp; str++; end1--; } } }
any idea ?
there absolutely nothing wrong reverse
implementation: code work long string pass null-terminated , writable.
then there must wrong way invoke it. common possibility passing string literal, writing undefined behavior may cause crash:
char *s = "quick brown fox"; reverse(s); // <<== undefined behavior
Comments
Post a Comment