Error Segmentation fault

Asked

Viewed 79 times

1

And having a problem with my job, I’m getting her address and I’m going through it, where it’s uppercase I convert to minuscule and wherever there’s a dot, exclamation, interrogation, two dots and a dot and comma.

But according to the Valgrind the problem is in line 82 and 155 of the code, ie in function diminuieeretira(); and in the main where I am receiving the return of the function diminuieretira();. I don’t understand what the problem is segmentation fault.

Below is the code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int contagemCaracteres(char* p) //Declaracao da funcao que conta quantos caracteres existem no texto
{
    int i = 0;

    while(p[i] != '\n')
    {
        i++;
    }
    return i;

}

void contagemPalavras(char* p , int r) //Declaracao de funcao que conta o numero de palavras existentes no texto
{
    int palavras = 0, parada = 0, i;


    for(i = 0; i < r ; i++)
    {
        if(p[i] >=  48 && p[i] <= 57 || p[i] >= 65 && p[i] <= 90 || p[i] >= 97 && p[i] <= 122)
        {
            palavras++;
            while(p[i] != 32)
            {
                parada++;
                i++;
            }
        }

    }

    printf("%d\n", palavras);
}


int  contagemFrases(char* p, int r)
{   
    int frases = 0, parada = 0, i;

    for(i = 0; i < r; i = parada + 1)
    {

        if(p[i] == 46 || p[i] == 33 || p[i] ==  63)
        {
            frases++;
            parada = i;
        }
        else if(p[i] == 32)
            parada = i;

        else if (p[i] >= 48 && p[i] <= 57 || p[i] >= 65 && p[i] <= 90 || p[i] >= 97 && p[i] <= 122)
        {
            if(i == 0)
                parada = 0;
            else
                parada = i;
        }
    }

    return frases;
}

void mediaCaracteres(int c, int f)
{
    float media = 0;

    media = (float)(c/f);

    printf("%.1f\n", media);
}

char* diminuieretira(char** p, int r)
{
    int i;

    for(i = 0; i < r; i++)
    {
        if(isupper(*p[i]))
            tolower(*p[i]);

        else if((*p[i]) == 46 || (*p[i]) == 44 || (*p[i]) == 59 || (*p[i]) == 58 || (*p[i]) == 33 || (*p[i]) == 63);
            (*p[i]) == 32;
    }

    printf("%s", (*p));
    return (*p);
}

void retiraespaco(char **p1, int r)
{
    int i , j , e = 0, counter = 1;
    char* paragrafo2 = NULL;

    for(i = 0; j < r; i++)
    {
        for(j = 0; j < r; j++)
        {
            if((*p1[j]) != 32)
            {
                paragrafo2 = (char*) realloc(paragrafo2, counter * sizeof(char));
                paragrafo2[i] = (*p1[j]);
                counter++;
                e = 0;
                break;
            }

            if((*p1[j]) == 32)
                e++;

            if(e == 1)
            {
                paragrafo2[i] = 32;
                break;
            }
        }

    }

    printf("%s\n", paragrafo2);
}

int main (void)
{
    int counter = 0, i = 0, recebe1, recebe2, caso;
    char* paragrafomod = NULL; 
    char* paragrafo = NULL;
    size_t tamalocado = 0;


    getline(&paragrafo, &tamalocado, stdin);
    do{
        scanf("%d", &caso);

        switch(caso)
        {
            case 1: //Primeira etapa
                recebe1 = contagemCaracteres(paragrafo);
                printf("%d\n", recebe1);

                contagemPalavras(paragrafo, recebe1);

                recebe2 = contagemFrases(paragrafo, recebe1);
                printf("%d\n", recebe2);

                mediaCaracteres(recebe1, recebe2);

            break;

            case 2:

                paragrafomod = diminuieretira(&paragrafo, recebe1); //Segudna etapa
                retiraespaco(&paragrafomod, recebe1);

            break;

            case 3: //Terceira etapa
            break;
        }   

    }while(caso != 0);


    free(paragrafo);

    return 0;
}
  • A tip, always use intuitive titles to the problem and organize by "topics", so it is more legible.

  • media = (float)(c/f); first makes the entire division c/f and sepois converts the result to the float. I think it should be media = (float)c/f; to make the division with floating comma numbers.

  • tolower(*p[i]); does not change value of any variable. You need to do *p[i] = tolower(*p[i]);

1 answer

0

char* diminuieretira(char** p, int r)
{
    int i;

    for(i = 0; i < r; i++)
    {
        if(isupper(*p[i]))
            tolower(*p[i]);

        else if((*p[i]) == 46 || (*p[i]) == 44 || (*p[i]) == 59 || (*p[i]) == 58 || (*p[i]) == 33 || (*p[i]) == 63);
            (*p[i]) == 32;
    }

    printf("%s", (*p));
    return (*p);
}

In this piece of code, the sub-expression *p[i] doesn’t do what you want. What you want is (*p)[i].

But for this function, you don’t need to complicate and pass a type value char **. Works well with a simple char *.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.