I have the following function used to count spaces in a string:
int countSpace(char *str) {
int nSpaces = 0;
while (*(str) != '\0') {
if (*(str) == ' ') {
nSpaces++;
}
str++;
}
return nSpaces;
}
I use this function like this:
char a[]="Hello ";
printf("%d", countSpace(a));
My question is, when I do this: printf("%s", a);, after calling countSpace, why is that a not pointing at the very end? I mean, I have incremented the pointer inside the countSpace function, but outside seems to be pointing at the very beginning still. Why is that?
I know that the changes that I make in *str inside the countSpace function will affect the outside, so that if for example I do: str[0] = 'p', outside of the function the value of a will be pola. So, I don't understand why the pointer is still pointing at the very beginning even though inside the function I have made it move forward.