Create chained list with realloc()

Asked

Viewed 211 times

0

I need to implement a simply chained list that does not have a next indication, that is, it should function as an array, accessing nearby positions in memory. I’m using the realloc() command to try to implement, however I’m getting an error when accessing some of the list items.

Struct that I’m wearing:

    typedef struct Lista lista;

    struct Lista {
    char nome[81];
    char telefone[15];
    char celular[15];
    char endereco[101];
    };

Function to insert in the list:

void insererealloc(lista **l, int t)
{
    *l = (lista*) realloc(*l, (t)*sizeof(lista));

    setbuf(stdin, NULL);

    printf("Digite o nome");
    gets((*l)->nome);

    setbuf(stdin, NULL);

    printf("Digite o telefone");
    gets((*l)->telefone);

    setbuf(stdin, NULL);

    printf("Digite o celular");
    gets((*l)->celular);

    setbuf(stdin, NULL);

    printf("Digite o endereco");
    gets((*l)->endereco);

    setbuf(stdin, NULL);
}

All clear so far! The problem occurs when I try to "print" the list elements, I am accessing them as a pointer, for example: list[i]->name, where i is any position. What is the correct way to access these members?

NOTE: I know that gets() is not at all unreliable, however I’m just doing tests and I got a little lazy using fgets() or something better.

  • With this alone I couldn’t find a way to help.

  • What information is missing?

  • What is the error that occurs when displaying? Post the line where you declare an element of type lista.

2 answers

0

when you take the position of a pointer it ceases to be a pointer. ex:

lista *l;
for(in=0;i<n;i++)
    puts(l[i]->nome); // isso vai dar erro

    puts(l[i].nome); // isso não vai dar erro

or

lista **l;
for(in=0;i<n;i++)
    puts(l[i]->nome); // isso não vai dar erro

    puts(l[i].nome); // isso vai dar erro

This depends on whether it is a double or single pointer, if it is simple, use [i]. and not [i]->

0

Good first a touch so that your code is more readable. When you’re working with struct and allocating them dynamically it’s always more interesting to put the typedef as a pointer, for example:

typedef struct Lista *lista;

So when you refer to the pointer created with the type lista, it will already become a pointer without needing the millions of *.

The most appropriate function for you to do this type of writing on a struct is undoubtedly getline and fgets. But be careful, fgets can generate buffer overflows that are due to the character ' n' that is supposed to be placed in the character vector, index always put +1 space in the vectors.

The main problem with the gets is that you have no control of how many bytes will be used or placed by the user. This can cause serious overflow problems.

Use the tips I gave and probably your headaches with vector readings of characters and structs will be lighter.

=)

Browser other questions tagged

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