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 

demo of working code.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -