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.
– Maniero
What information is missing?
– Vinicius
What is the error that occurs when displaying? Post the line where you declare an element of type
lista
.– DaviAragao