Error segmentation fault

1

And having a problem with my function, I'm getting her address and I'm going through it, where it's a capital I convert to minuscule and where I have a point, an exclamation mark, a colon, a colon and a semicolon, / p>

But according to valgrind the problem is in line 82 and 155 of the code, that is, the function diminuieeretira(); and the main where I'm getting the return of the function diminuieretira(); . I'm not understanding what the problem is with giving segmentation fault .

The following is the code below:

#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;
}
    
asked by anonymous 10.06.2015 / 21:59

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 subexpression *p[i] does not do what you want. What do you want 'and (*p)[i] .

But, for this function, you do not need to complicate and pass a value of type char ** . It works well with a simple char * .

    
10.06.2015 / 23:26