/* squeeze: delete all c from s */
void squeeze(char s[], int c)
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
if (s[i] != c)
s[j++] = s[i];
s[j] = '\0';
}
int main(void)
{
squeeze("squeeze", 'z');
return 0;
}
I compiled it with gcc and ran it, and got a segmentation fault as a result.
Anything wrong with this example?
thanks to men,i have just made a usual mistake.
Your example shows that you’re trying to apply
squeeze()to a string literal ("squueze"). This is not correct, since string literals are not always modifiable so it’s invalid to tryto modify them. You need to call it with a character array: