0
int main(int argc, char const *argv[])
{
    LISTA *lista1;
    LISTA *lista2;
    crearLista(lista1);
    crearLista(lista2);
    STR *s = crearSTR("Hola");    // sin restricción de longitud
    NUMBER *n = crearNRO(10);     // solo usar enteros.
    agregarLista(lista1, s);    // el orden de adición define
    agregarLista(lista1, n);    // lo que se quiere.
    agregarLista(lista1, lista2); // lista de lista
    destruirSTR(s);
    destruirNRO(n);
}

In LISTA.c

void crearLista(LISTA *Lista)
{
    LISTA nueva;
    *Lista=nueva;
}

LISTA.h

typedef struct lista LISTA;
void crearLista(LISTA *Lista);
void agregarLista(LISTA *Lista, void *elem);

This gives me a segmentation fault. In gdb the lista1 variable is initialized in 0x0 and I don't understand why.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Andythem
  • 13
  • 4
  • 1
    Ask yourself - what is the value of `Lista` that is passed into the function? It's an unintialised pointer so of course dereferencing it will lead to bad things happening. See the duplicate post for more details and options for how to do it correctly. – kaylum Jul 27 '21 at 00:57
  • No, I know I can do a double pointer, but my teacher says it can be done that way, my "crearLista" function is wrong – Andythem Jul 27 '21 at 00:59
  • "*my teacher says*". What exactly did the teacher say? Is it not possible you misunderstood? If that really must be the signature of your function then you need to call it like this: `LISTA lista1; crearLista(&lista1);` – kaylum Jul 27 '21 at 01:01
  • I don't know the language - but I _think_ you are trying the wrong thing. `crearLista` doesn't crear anything. You may also want to present the definition of `LISTA` so we know what it is – Ted Lyngmo Jul 27 '21 at 01:01
  • Please try presenting a [mre] – Ted Lyngmo Jul 27 '21 at 01:02
  • The code I have in main is the example code that my teacher gave me. The code I wrote is in the libraries, STR.h, STR.c, NRO.c, NRO.h, LISTA.h and LISTA.c – Andythem Jul 27 '21 at 01:04
  • 1
    Then it appears that your teacher is wrong. There is no way passing an uninitalised pointer to a function is the right thing to do. Alternatively there is something you have misunderstood - would suggest clarifying with the teacher. – kaylum Jul 27 '21 at 01:07
  • I understand, I talked to him, but he says he can't help me, it is my assignment. Thanks anyway, if you make that an answer I will mark as solved. – Andythem Jul 27 '21 at 01:11
  • Or post a [mre] so that anyone of us can help you for real. It's no use if you have us guessing. – Ted Lyngmo Jul 27 '21 at 01:14
  • `LISTA *lista1;` --> `LISTA *lista1 = &(LISTA){0};` – chux - Reinstate Monica Jul 27 '21 at 03:47

1 Answers1

-2

Your error is in the function crearLista() ...

Where does the value of nueva come from? (It doesn't ...)

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41