Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [C] Errore nell ' esecuzione di una lista di interi in C

    Salve a tutti, ho aperto questo topic a causa di un errore che ho scovato in fase di compilazione di un mio progetto in linguaggio C che ha il semplice compito di creare una lista di interi senza segno avente i valori immessi dall ' utente e visualizzarla a video. Posto il codice cosicchè possiate scrutare il mio progetto in cerca
    dell ' errore da me non capito :

    codice:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    typedef struct Structure {
            unsigned int Value;
            struct Element * Next;
    }Element;
    
    Element * List;
    
    Element * Create_List (  );
    
    void View_List ( Element * List );
    
    int main (  )
    {
        List = Create_List (  );
        system ( "CLS" );
        View_List ( List );
        system ( "PAUSE" );
        return 0;
    }
    
    Element * Create_List (  )
    {
            Element * Pointer, * Scroller;
            unsigned int Elements;
            printf ( "How many items you want to create ? " );
            scanf ( " %u ", & Elements );
            if ( Elements == 0 )
            Pointer = NULL;
            else if ( Elements > 0 )
            {
                Pointer = ( Element * ) malloc ( sizeof (Element ) );
                printf ( "Insert the value of the element : " );
                scanf ( " %u ", & Pointer -> Value );
                Scroller = Pointer;
                if ( Elements > 1 )
                {
                     for ( ; ( Elements - 1 ) >= 0 ; Elements-- )
                     {
                         Scroller -> Next = ( Element * ) malloc ( sizeof (Element ) );
                         Scroller = Scroller -> Next;
                         printf ( "Insert the value of the element : " );
                         scanf ( " %u ", & Pointer -> Value );
                     }
                }
                Scroller -> Next = NULL;
            }
            return Pointer;
    }
    
    void View_List ( Element * List )
    {
         printf ( "List --->" );
         while ( List != NULL )
         {
               printf ( " %u --->", List -> Value );
         }
         printf ( " NULL\n\n" );
    }
    Spero che leggendo questo codice da me scritto riuscirete
    a scovare gli errori che ahimè, io non sono riuscito a capire...

    Ringrazio tutti per l'aiuto dato

  2. #2
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Dici di aver un problema con la compilazione... Che errore ottieni? Dove?
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  3. #3
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466
    codice:
    typedef struct Structure {
            unsigned int Value;
            struct Structure * Next;
    } Element;
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  4. #4
    Grazie dell ' aiuto ma per fortuna ho scovato da solo
    i miei benedetti errori del codice sorgente, vi posto il tutto :

    codice:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    typedef struct Element {
            unsigned int Value;
            struct Element * Next;
    }Element;
    
    Element * Create_List (  );
    
    void View_List ( Element * List );
    
    int main (  )
    {
        Element * List;
        List = Create_List (  );
        system ( "CLS" );
        View_List ( List );
        system ( "PAUSE" );
        return 0;
    }
    
    Element * Create_List (  )
    {
            Element * Pointer, * Scroller;
            unsigned int Elements;
            printf ( "How many items you want to create ? " );
            scanf ( "%u", & Elements );
            if ( Elements == 0 )
            Pointer = NULL;
            else if ( Elements > 0 )
            {
                Pointer = ( Element * ) malloc ( sizeof (Element ) );
                printf ( "Insert the value of the element : " );
                scanf ( "%u", & Pointer -> Value );
                Scroller = Pointer;
                if ( Elements > 1 )
                {
                     for ( ; ( Elements - 1 ) >= 0 ; Elements-- )
                     {
                         Scroller -> Next = ( Element * ) malloc ( sizeof (Element ) );
                         Scroller = Scroller -> Next;
                         printf ( "Insert the value of the element : " );
                         scanf ( "%u", & Scroller -> Value );
                     }
                }
                Scroller -> Next = NULL;
            }
            return Pointer;
    }
    
    void View_List ( Element * List )
    {
         printf ( "List --->" );
         while ( List != NULL )
         {
               printf ( " %u --->", List -> Value );
         }
         printf ( " NULL\n\n" );
    }
    Ringrazio tutti per l ' aiuto che mi avete dato...

  5. #5
    Utente di HTML.it
    Registrato dal
    Jul 2008
    Messaggi
    1,326
    Questo

    codice:
    unsigned int Elements;
    ...
    for ( ; ( Elements - 1 ) >= 0 ; Elements-- )
    non può andare... "Elements" è un intero senza segno, quindi risulterà sempre >= 0 e quel ciclo procederà all'infinito. Ridefinisci la variabile come intera con segno oppure modifica in qualche modo il ciclo.

    Inoltre

    codice:
    void View_List ( Element * List )
    {
         printf ( "List --->" );
         while ( List != NULL )
         {
               printf ( " %u --->", List -> Value );
    	   List = List -> Next;
         }
         printf ( " NULL\n\n" );
    }
    se non fai quella modifica visiterai all'infinito sempre il primo elemento della lista. Tra l'altro è lo stesso errore che avevi commesso in un altro programma qualche giorno fa, fai attenzione...
    every day above ground is a good one

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.