So, its a pretty simple problem and I know the solution which is a simple function like the one below:
void removeSpaces(char* s) {
char* source = s;
char* dest = s;
while(*source) {
if(*source == ' ') {
source++;
} else {
*dest++ = *source++;
}
}
*dest = 0;
}
I am working in Visual C++ 2008 Express edition
When I call it with the following it works fine without any issues i.e it removes all the spaces:
int main() {
char input[50] = "I like 2% milk";
removeSpaces(input);
cout<<input;
getchar();
return 0;
}
But, the problem is when I call it by changing the string declaration to this:
char * input = "I like 2% milk";
I get a exception (some kind of access violation)
The exception is showing on this line of code of the removeSpace function
*dest++ = *source++;
Can anyone elaborate as to why is this happening?