Salve a tutti,
sto cercando di scrivere una funzione ricorsiva che prendendo un array come argomento mi restituisca una lista. il codice che segue è il meglio che ho potuto fare:
Non riesco a capire dove sbaglio perché il programma mi stampa:codice:#include <stdio.h> typedef struct lista { int num; struct lista *suc; } Ls; Ls* creaListaDaArray(int array[]) { Ls* testa; if (array[0] == 0) return(NULL); else { testa=(Ls*)malloc(sizeof(Ls)); testa->num = array[0]; testa->suc = creaListaDaArray(array+1); return(testa); } } void stampaLista(Ls *testa) { if (testa == NULL) { printf("NULL"); return; } while (testa != NULL) { printf("%d ", testa->num); testa = testa->suc; } } main() { int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Ls *testa; testa = creaListaDaArray(array); stampaLista(testa); }
1 2 3 4 5 6 7 8 9 10 7209077 7733353 74 2 2293680 4198887 1 4143072 4139368 4210
688 2293668 -1 2293672 -2126502240 -1 4139368
Inoltre se un elemento del array fosse pari a 0 la funzione creaListaDaArray non funzionerebbe... Sarei molto grato se qualcuno mi desse una mano a risolvere il problema.

Rispondi quotando