"Pure" structs (no pointer) and C error handling

Asked

Viewed 316 times

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?

  • 1

    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.

  • Some of these answers answered him?

4 answers

2

If you cannot change the function to return a pointer and return NULL in case of error, I suggest an element with 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;
}
  • Good suggestion this first for the case of demanding people.

0

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

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

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

0

Retouch NULL.

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

void foo(){
    if(retornaStruct() == NULL){ // Com erro, retornado NULL
        printf("ERRO!!!!\n\n\n");
    }
}
  • That’s the thing, if I used pointer to struct it would be easy, because I would do what you said, I would return NULL in the function.. The problem is if I wanted to work with structs without the pointer, you know?

0

Code removing a Node, in it it is possible to see the return types, see that the return is a noL (in the header noL *remove_no_lista) and that there is a return NULL at the end of the post return of structs type noL (specified type of return).

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;
    }

Browser other questions tagged

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