Attempted to read or write protected memory : C++ Modified Value of Memory -
this question has answer here:
i trim string "char array in c , c++ or char pointer" use function:
inline char * trimright(char * str) { char * end = str + strlen(str); while(str != end) { end--; switch(*end) { case ' ': case '\t': case '\n': case '\v': case '\f': case '\r': break; default: *(end+1) = '\0'; return end+1; } } return str; }
but return error (reason in code *(end+1) = '\0' ) :
an unhandled exception of type 'system.accessviolationexception' occurred in x.exe
additional information: attempted read or write protected memory. indication other memory corrupt.
please me.
no doubt tried call function on string literal, write protected. make sure don't use string literals , fine.
bad:
char *s = "hello "; trimright(s);
good:
char s[] = "hello "; trimright(s);
also way, function doesn't trim if string spaces. i'm not sure if wanted way, seems should set *str = '\0';
in case.
Comments
Post a Comment