Error comparing two strings

Asked

Viewed 218 times

2

I’m trying to compare two strings, when I enter a name in my chained list it cannot be entered if it already exists. I have used the function searchname to display a searched name and to delete, but now it is simply giving Segmentation fault on the line calling the strcmp(). Following is code for the problem functions.

void insere_inicio(celula *nodo) {
    celula *teste;
    printf("\t Nome: ");
    ler_string(busca.nome, 30);
    teste = busca_nome( nodo);
    if (teste == NULL) {
        celula *info;
        info = malloc(sizeof (struct cel));
        strcpy(info->nome, busca.nome);
        printf("\t Rua: ");
        ler_string(info->rua, 40);
        printf("\t Cidade: ");
        ler_string(info->cidade, 20);
        printf("\t Estado: ");
        ler_string(info->estado, 2);
        printf("\t CEP: ");
        scanf("%lu", &info->cep);
        info -> prox = nodo -> prox;
        nodo -> prox = info;
    } else {
        printf("Esse nome já está cadastrado !!");
    }
    free(teste);
}

/*
 * função que faz a busca de um nome no programa
 * le o nome que está localizado na struct busca.nome
 * se encontra um nome igual retorna o ponteiro para o nome
 * caso não encontre retorna um ponteiro NULL
 */
celula *busca_nome(celula *inicio) {
    int flag = 0;
    celula *info;
    celula *anterior;
    do {
        if (flag == 0) {
            anterior = inicio;
            info = inicio -> prox;
            flag = 1;
        } else {
            anterior = info;
            info = info -> prox;
        }
        if (strcmp(busca.nome, info -> nome) == 0)
            return anterior;
    } while (info -> prox != NULL);

    return NULL;
}

1 answer

3


The code is full of problems. I will point out some:

The main visible is memory corruption. Variables info and anterior were declared but memory was not allocated to them. When you tried to write something on them it dulled the memory, possibly wrote on top of something important.

I also don’t know where the variable came from busca. It does not exist in this scope. Does it exist in a larger scope? Even if it does exist this is not an appropriate practice in most cases.

That one if with flag doesn’t make much sense to me, but it probably works.

And in the other function you try to free the memory of something that has never been allocated.

Tip: whenever possible (almost always) use the malloc() and the free() that releases it in the same function. Using them separately is looking for confusion. Even experienced programmers get lost when they do this.

Some other technical things are strange but without seeing the whole I’m not sure. The algorithm as a whole doesn’t seem to make much sense.

  • I will correct the pointed errors, if with flag is used because I have the first cell just as head, and I don’t use the same, so for when I’m going through the loop the first time I have to skip it and not read the same...

  • 1

    But if it will run only the first time, then run out of the do and reverses the order of else with the ifbottom. The algorithm becomes much simpler. Almost always when you have to use a flag there’s something wrong with the algorithm.

Browser other questions tagged

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