Pointers are special type of variables used to store address of other variables. when you changed value inside the str pointer with "str++", it then pointed to the next element and after the while loop str pointed to the last element of the string('\0'). So you must store the the address of the first character of the string to do something with it later.
int main() {
char *s = "abcde", *str =s; // s,str stores address of first character
while(*str!='\0'){
if(*str ==' ')
*str='-';
printf("%c", *str);
str= str+1; // str now points to the next character. But s remains unchanged
}
}