segmentation fault

4

I have a problem when I try to assign a character to my 'next' pointer, it throws me a segmentation error. With the debugger I've seen that next points to a character, the only thing I want to do is step on that character with a blank space (''). I do not understand why the error, since next is initialized with my pointer s1, which does not come NULL, but also corroborated eh that has not overflowed the chain.

int deleteChar(char *s1,const char *s2)
{
  char *first = s1,
       *last = s1,
       *next = s1;
  int accumulator = 0;

while(*next)
{
    while(isWhite(*next) && *next)
        next ++;
    while(*next && !isWhite(*next))
    {
        if(isCharacter(*next, s2))
        {
            *next = ' '; //error de segmentacio
            accumulator ++;
        }
        next ++;
    }
    while(last < next)
    {
        if(*last != ' ')
        {
            *first = *last;
            first ++;
        }
        last ++;
    }
    *first = ' ';
    first ++;
}
first --;
*first = '
int deleteChar(char *s1,const char *s2)
{
  char *first = s1,
       *last = s1,
       *next = s1;
  int accumulator = 0;

while(*next)
{
    while(isWhite(*next) && *next)
        next ++;
    while(*next && !isWhite(*next))
    {
        if(isCharacter(*next, s2))
        {
            *next = ' '; //error de segmentacio
            accumulator ++;
        }
        next ++;
    }
    while(last < next)
    {
        if(*last != ' ')
        {
            *first = *last;
            first ++;
        }
        last ++;
    }
    *first = ' ';
    first ++;
}
first --;
*first = '%pre%';

    return accumulator;
  }

int isCharacter(const char c, const char *s)
{
  while(c != *s && *s)
    s ++;

  return c == *s ? 1 : 0;
}

#define isWhite(X) ((X) == ' ' || (X) == '\t')

int main()
{
char *s1 = {"algo que se me ocurrió"},
     *s2 = {"aeiou"};
int cant;
printf("Prueba eliminar caraacter\n");

puts(s1);
puts(s2);
cant = deleteChar(s1,s2);
printf("La cadena modificada es: %s",s1);
printf("Cantidad de caracteres eliminados: %d",cant);

return 0;
}
'; return accumulator; } int isCharacter(const char c, const char *s) { while(c != *s && *s) s ++; return c == *s ? 1 : 0; } #define isWhite(X) ((X) == ' ' || (X) == '\t') int main() { char *s1 = {"algo que se me ocurrió"}, *s2 = {"aeiou"}; int cant; printf("Prueba eliminar caraacter\n"); puts(s1); puts(s2); cant = deleteChar(s1,s2); printf("La cadena modificada es: %s",s1); printf("Cantidad de caracteres eliminados: %d",cant); return 0; }
    
asked by Jorge Gonzalez 11.09.2016 в 15:43
source

1 answer

2

The problem is in the first line of main

char *s1 = {"algo que se me ocurrió"},

The variable s1 is a pointer to a string literal. Trying to modify it is indefinite behavior.

To have a modifiable string, you must declare it like this:

char s1[] = {"algo que se me ocurrió"},

Now s1 is an array of characters and you can modify it. And, although now s1 is not a pointer, it can be used anywhere (*) where a character pointer is needed because it is evaluated as a pointer to the first character of this modifiable string.

(*) not really, for example you can not take the address of s1; this would also be undefined behavior char**p = &s1;*p = 'a'; if s1 is an array. But this would be the subject of another question.

    
answered by 11.09.2016 / 16:11
source