Error declaration null - Unknown type name bool

Asked

Viewed 1,567 times

4

The error you are making is in this function.

bool tem_numero_na_lista(tipo_lista * aux, int valor) {
  while (aux - > prox != NULL) {
    if (aux - > info == valor) {
      return true;
    }
    aux = aux - > prox;
    else {
      return false;
    }
  }
}

error: Unknown type name 'bool'

  • 1

    you included #include <stdbool.h>?

2 answers

5

Added #include <stdbool.h>? Using a compiler at least C99 compatible? Need.

This code has other problems.

Documentation.

1


In addition to error: Unknown type name 'bool'| has some other minor errors in function

while (aux -> prox != NULL)

Will not check the last item in the list.

else {
  return false;
}

The else is not necessary, to return false if the list has been searched and the value not found, just put the return after the while.

bool tem_numero_na_lista(tipo_lista * aux, int valor) {
  while (aux != NULL) {
    if (aux -> info == valor) {
      return true;
    }
    aux = aux -> prox;
  }
  return false;
}

Already having a lista with the entered values, you can test like this:

Ex: Check if number 7 is in the list.

int valor = 7;
if( tem_numero_na_lista(lista, valor) )
    printf("Encontrou o valor %d na lista", valor);
else
    printf("Não encontrou o valor %d na lista", valor);
  • It is worth remembering that the arrow operator -> doesn’t admit space between the - and the >; put spaces there cause syntax error when reading the code.

  • as I test on @Lucas Trigueiro?

  • 1

    I edited the answer by adding a test example.

Browser other questions tagged

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