I am trying to write a program from removing spaces in a string in C. I tried to debug using gdb
but I guess I am not very knowledgable in using that tool. My code is as follows:
#include<stdio.h>
void RemoveSpaces(char * str){
char *var1, *var2;
var1 = str; var2 = str;
while(*var1 != '\0'){
if(*var1 != ' '){
*var2 = *var1;
var2++;
}
var1++;
}
*var2 = '\0';
}
int main(){
char *str = "abc de";
printf("Before: %s\n", str);
RemoveSpaces(str);
printf("After: %s\n", str);
return 0;
}
I tried to set watch on var1 and var2 but I get SIGSEGV on line where *var2 = *var1;
I looked for some code here before writing my own. Of course, the answer states that its untested but I am doing a similar statement on the line where I get error. Why wouldn't (thereotically) *i = *j++;
give an error while *var2 = *var1;
would? (I don't want someone to remove my error. I just want to know why I might be getting this error so that I can avoid it in future. )