"Pure" structs (without pointer) and error handling in C

1

Stirring with data structures ( filas , pilhas , etc), I got stuck trying to create search functions for these structures.

By default, functions that may contain errors return 0 or -1 when the return type is an integer.

  

But what if the return type of a function is a struct and an error was encountered?

When searching for a given 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 she is not found? I return what?

    
asked by anonymous 24.05.2015 / 02:35

4 answers

2

If you can not change the function to return a pointer and return NULL in case of an error, I suggest an element with a special value

const struct registro especial_erro = {-1, ...};
struct registro busca(Arvore a, int valor) {
    /* ... */
    if (erro) return especial_erro;
}

If you can change the function, use NULL to indicate error

struct registro *busca(Arvore a, int valor) {
    /* ... */
    if (erro) return NULL;
}
    
24.05.2015 / 09:09
0

Retry NULL .

Since the return will be of type Struct, return NULL and make a check similar to this:

void foo(){
    if(retornaStruct() == NULL){ // Com erro, retornado NULL
        printf("ERRO!!!!\n\n\n");
    }
}
    
24.05.2015 / 02:54
0

Node removal code, it is possible to see the return types, see that the return is noL (in the header noL *remove_no_lista ) and that there is a return NULL at the end of the function after several return of structs type noL (return type specified).

typedef struct No
    {
        char info;
        int numeracao; // facilita a busca em caso de duplicacao
        struct No *esq,*dir;
    }no;
typedef struct NoL
    {
        no *noA;
        struct NoL *prox;
    }noL;

noL *remove_no_lista(noL *raiz,noL *noLista)
    {
        if(noLista != NULL)
            {
               noL *p = NULL;
               if(noLista == raiz)//caso seja raiz
                    {
                        p = raiz;
                        raiz = raiz->prox;
                        free(p);
                        return raiz;
                    }
                else
                    {
                        if(noLista->prox == NULL) //caso tenha 2 ou mais na lista
                            {
                                for(p = raiz; p->prox->prox != NULL; p = p->prox);
                                p->prox = NULL;
                                free(noLista);
                                return raiz;
                            }
                        else
                            {
                                        for(p = raiz; p->prox != noLista; p = p->prox);//meio da lista
                                        p->prox = noLista->prox;
                                        free(noLista);
                                        return raiz;
                            }
                    }
            }
        else
            return NULL;
    }
    
24.05.2015 / 14:24
0

You must use pointers to return queries in the dynamic data structure. Even because it is made and assembled entirely with their help. Make

void *busca(Arvore a, int valor); /* o poder do casting do void em C: 
                                     utilize ele! */

And return NULL if it does not find the value. Return a pointer to the desired structure if you find such a value in your tree.

    
27.05.2015 / 04:13