1
Messing with data structures (filas
, pilhas
, etc), I reached an impasse in trying to create search functions for these structures.
By default, functions that can contain errors return 0 or even -1 when the return type is an integer.
But what if the return type of a function is a struct and an error has been found?
When searching for a certain type of data in a tree, for example, in a function with the following prototype:
typedef struct registro {
int valor;
// outros dados...
} Registro;
// Supondo que o tipo Árvore já foi criado
Registro busca(Arvore a, int valor);
If the record is found in the tree, beauty, returns the struct that contains this value. But what if it is not found? Return what?
The most recommended thing is to think about pointers - the idea of returning the structs themselves may seem simpler - but inside the program strange things happen: either you manage the memory allocation for your struct manually, or, if it is a variable declared within its function, the values of the function returned from the previous call can be changed by a new call to the function.
– jsbueno
Some of these answers answered him?
– Ricardo