Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2009
    Messaggi
    7

    [c] Puntatori e passaggio di parametri in una libreria .h

    Salve, sono in difficoltà in quanto devo creare una libreria per la gestione di grafi. :master:
    In questa libreria ho definito alcune strutture, ve ne posto una per rappresentanza:

    struct ListaAdiacenza{
    char Info;
    int Peso;
    struct ListaAdiacenza *PuntDestra;
    struct ListaAdiacenza *PuntSotto;
    };
    In seguito ho creato il seguente alias (anche se non so quanto ciò sia lecito):

    typedef ListaAdiacenza *PuntLista;
    All'interno della stessa libreria ho implementato delle funzioni e delle procedure, ad esempio:

    void AggiungiNodo(PuntLista PuntStart, char InfoNodo){
    //Variabili locali:
    PuntLista PuntCorrente;
    PuntLista PuntNuovo;
    PuntNuovo = (PuntLista) malloc(sizeof(ListaAdiacenza)); //Chiede spazio in memoria
    if(PuntNuovo != NULL){
    PuntNuovo->Info = InfoNodo;
    PuntNuovo->Peso = 0;
    PuntNuovo->PuntDestra=NULL;
    PuntNuovo->PuntSotto=NULL;
    PuntCorrente = PuntStart;
    if(PuntCorrente==NULL)
    PuntStart=PuntNuovo;
    else{

    ...
    Ho provato a fare un programmino per verificare il funzionamento della libreria ma tra i vari errori ho:

    (1) In file included from C:/Documents and Settings/Davide/Desktop/Progetto/algoritmi.c (in corrispondenza dell'include della mia libreria nel programma .c di prova)
    (2) Parse error before "*" token in corrispondenza dei typedef
    (3) Parse error before "PuntCorrente" in corrispondenza di un po' tutti i puntatori
    Altri errori di variabili locali che vede come non definite...

    Se qualcuno può aiutarmi gli sarei grato. Grazie mille

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Mostra il .h completo e i messaggi d'errore del compilatore
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2009
    Messaggi
    7
    Non l'ho fatto prima perchè mi sembrava un po' troppo lungo e un po' troppi errori. Comunque eccolo:

    #ifndef _GRAFO_H
    #define _GRAFO_H


    //LIBRERIE:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>


    //STRUTTURE:
    struct ListaAdiacenza{//struttura per gli elementi della lista di adiacenza
    char Info;//nome del nodo
    int Peso;//peso dell'arco entrante dal nodo relativo al primo elemento della riga della lista di adiacenza
    struct ListaAdiacenza *PuntDestra;//puntatore all'elemento successivo della riga
    struct ListaAdiacenza *PuntSotto;//puntatore all'elemento successivo della colonna (!=NULL solo per la prima colonna)
    };

    struct ListaNodi{//struttura per insiemi di nodi
    char Info;//nome del nodo
    struct ListaNodi *PuntProssimo;//puntatore all'elemento successivo
    };

    struct ListaArchi{//struttura per gli elementi della lista di adiacenza
    char InfoUscente;//nome del nodo in cui l'arco è uscente
    char InfoEntrante;//nome del nodo in cui l'arco è entrante
    int Peso;//peso dell'arco
    struct ListaArchi *PuntProssimo;//puntatore all'elemento successivo
    };


    //ALIAS
    typedef ListaAdiacenza *PuntLista; //Assegnazione dell'alias PuntLista a ListaAdiacenza *
    typedef ListaNodi *PuntNodi; //Assegnazione dell'alias PuntNodi a ListaNodi *





    //FUNZIONI E PROCEDURE



    //AggiungiNodo
    void AggiungiNodo(PuntLista PuntStart, char InfoNodo){
    //Variabili locali:
    PuntLista PuntCorrente;
    PuntLista PuntNuovo;
    PuntNuovo = (PuntLista) malloc(sizeof(ListaAdiacenza)); //Chiede spazio in memoria
    if(PuntNuovo != NULL){
    PuntNuovo->Info = InfoNodo; //Viene inserito il carattere nel campo Info
    PuntNuovo->Peso = 0; //Viene posto il peso del primo elemento di ogni riga a 0
    PuntNuovo->PuntDestra=NULL;
    PuntNuovo->PuntSotto=NULL;
    PuntCorrente = PuntStart;
    if(PuntCorrente==NULL)
    PuntStart=PuntNuovo;
    else{
    //Si scende al nodo in basso fin quando non abbiamo NULL
    while(PuntCorrente->PuntSotto!=NULL)
    PuntCorrente=PuntCorrente->PuntSotto;
    PuntCorrente->Sotto = PuntNuovo;
    }
    }




    //AggiungiArco
    bool AggiungiArco(PuntLista PuntStart, char InfoPartenza, char InfoArrivo, int PesoArco){
    //Variabili locali:
    bool errore = true;
    PuntLista PuntCorrente;
    PuntLista PuntRiga;
    PuntLista PuntNuovo;
    PuntNuovo = (PuntLista) malloc(sizeof(ListaAdiacenza)); //Chiede spazio in memoria
    if(PuntNuovo != NULL){
    PuntNuovo->Info = InfoArrivo; //Viene inserito il carattere nel campo lettera
    PuntNuovo->Peso = PesoArco; //Viene inializzato lo stato della lettera a non marcato (0)
    PuntNuovo->PuntDestra = NULL;
    PuntNuovo->PuntSotto = NULL;
    PuntCorrente = PuntStart;
    PuntRiga = PuntStart;
    while(PuntRiga!=NULL){
    if(PuntRiga->Info == InfoPartenza){
    PuntCorrente = PuntRiga;
    while(PuntCorrente->PuntDestra!=NULL)
    PuntCorrente = PuntCorrente->PuntDestra;
    PuntCorrente->PuntDestra = PuntNuovo;
    errore = false;
    PuntRiga = NULL;
    }
    else
    PuntRiga = PuntRiga->PuntSotto;
    }
    return errore;
    }





    //EliminaArco
    bool EliminaArco(PuntLista PuntStart, char InfoPartenza, char InfoArrivo){
    //Variabili locali:
    bool errore = true;
    PuntLista PuntCorrente;
    PuntLista PuntPrecedente;
    PuntLista PuntRiga;
    PuntLista PuntEliminatore;
    PuntRiga = PuntStart;
    while(PuntRiga!=NULL){
    if(PuntRiga->Info == InfoPartenza){
    PuntCorrente = PuntRiga->PuntDestra;
    PuntPrecedente = PuntRiga;
    while(PuntCorrente!=NULL){
    if(PuntCorrente->Info == InfoArrivo){
    PuntEliminatore=PuntCorrente;
    PuntPrecedente->PuntDestra = PuntCorrente->PuntDestra;
    free(PuntEliminatore);
    errore = false;
    PuntCorrente = NULL;
    PuntRiga = NULL;
    }
    else{
    PuntPrecedente=PuntCorrente;
    PuntCorrente=PuntCorrente->PuntDestra;
    }
    }
    }
    else
    PuntRiga = PuntRiga->PuntSotto;
    }
    return errore;
    }





    //EliminaNodo
    bool EliminaNodo(PuntLista PuntStart, char InfoNodo){ //Elimina il nodo e tutti gli archi connessi
    //Variabili locali:
    PuntLista PuntCorrente;
    PuntLista PuntPrecedente;
    PuntLista PuntRiga;
    PuntLista PuntEliminatore;
    PuntLista PuntEliminatoreSucc;
    PuntPrecedente = PuntStart;
    PuntCorrente = PuntStart->PuntSotto;
    bool errore = true;
    bool uscita;
    //Si scende al nodo in basso fin quando non abbiamo NULL
    while(PuntCorrente!=NULL){
    if(PuntCorrente->Info == InfoNodo){
    PuntEliminatore = PuntCorrente;
    PuntEliminatoreSucc = PuntEliminatore->PuntDestra;
    PuntPrecedente->PuntSotto = PuntEliminatore->Sotto;
    free(PuntEliminatore); //Elimina il primo elemento della riga
    while(PuntEliminatoreSuccessivo != NULL){ //Elimina tutti gli elementi della riga
    PuntEliminatore = PuntEliminatoreSuccessivo;
    PuntEliminatoreSuccessivo = PuntEliminatoreSuccessivo->PuntDestra;
    free(PuntEliminatore);
    }
    PuntCorrente = NULL;
    errore = false;
    }
    else{
    PuntPrecedente = PuntCorrente;
    PuntCorrente = PuntCorrente->PuntSotto;
    }
    }
    PuntRiga=PuntStart;
    while(PuntRiga!=NULL){
    uscita = EliminaArco(PuntStart, PuntRiga->Info, InfoNodo);
    PuntRiga = PuntRiga->Sotto;
    }
    return errore;
    }





    //LunghezzaCammino
    int LunghezzaCammino(PuntNodi PrimoNodo, PuntLista PuntStart){
    PuntNodi PuntNodoPartenza;
    PuntNodi PuntNodoArrivo;
    PuntLista PuntRicerca;
    int lunghezza = 0;
    bool trovato;
    bool errore = false;

    if(PrimoNodo!=NULL){
    PuntNodoPartenza = PrimoNodo;
    PuntNodoArrivo = PrimoNodo->PuntProssimo;
    while(PuntNodoArrivo != NULL){
    PuntRicerca = PuntStart;
    trovato=false;
    while(PuntRicerca!=NULL){
    if(PuntRicerca->Info == PuntNodoPartenza->Info){
    while(PuntRicerca!=NULL){
    if(PuntRicerca->Info == PuntNodoArrivo->Info){
    lunghezza = lunghezza + PuntRicerca->Peso;
    PuntRicerca = NULL;
    trovato = true;
    }
    PuntRicerca = PuntRicerca->PuntDestra;
    }
    }
    PuntRicerca = PuntRicerca->PuntSotto;
    }
    if(trovato == false)
    errore=true;
    PuntNodoPartenza = PuntNodoArrivo;
    PuntNodoArrivo = PuntNodoArrivo->PuntProssimo;
    }
    }
    if(errore == false)
    return -1;
    else
    return lunghezza;
    }




    #endif
    Errori:

    [QUOTE]

    3 C:\Documents and Settings\Davide\Desktop\Progetto\algoritmi.c In file included from C:/Documents and Settings/Davide/Desktop/Progetto/algoritmi.c

    39 C:\Documents and Settings\Davide\Desktop\Progetto\grafo.h parse error before '*' token

    39 C:\Documents and Settings\Davide\Desktop\Progetto\grafo.h [Warning] data definition has no type or storage class

    40 C:\Documents and Settings\Davide\Desktop\Progetto\grafo.h parse error before '*' token

    40 C:\Documents and Settings\Davide\Desktop\Progetto\grafo.h [Warning] data definition has no type or storage class

    51 C:\Documents and Settings\Davide\Desktop\Progetto\grafo.h parse error before "PuntStart"

    C:\Documents and Settings\Davide\Desktop\Progetto\grafo.h In function `AggiungiNodo':

    53 C:\Documents and Settings\Davide\Desktop\Progetto\grafo.h parse error before "PuntCorrente"

    55 C:\Documents and Settings\Davide\Desktop\Progetto\grafo.h `PuntNuovo' undeclared (first use in this function)

    ...

    [QUOTE]

    Gli errori sono davvero tanti ma tutti simili...

    Grazie ancora, spero possiate aiutarmi.

  4. #4
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Dato che stai usando il C e non il C++, dovresti scrivere

    typedef struct ListaAdiacenza *PuntLista;
    typedef struct ListaNodi *PuntNodi;


    P.S. I file .h non sono LIBRERIE ma semplici include
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  5. #5
    Utente di HTML.it
    Registrato dal
    Aug 2009
    Messaggi
    7
    Facendo in questo modo:

    //STRUTTURE:
    struct ListaAdicenza{//struttura per gli elementi della lista di adiacenza
    char Info;//nome del nodo
    int Peso;//peso dell'arco entrante dal nodo relativo al primo elemento della riga della lista di adiacenza
    struct ListaAdiacenza *PuntDestra;//puntatore all'elemento successivo della riga
    struct ListaAdiacenza *PuntSotto;//puntatore all'elemento successivo della colonna (!=NULL solo per la prima colonna)
    };

    struct ListaNodi{//struttura per insiemi di nodi
    char Info;//nome del nodo
    struct ListaNodi *PuntProssimo;//puntatore all'elemento successivo
    };

    //ALIAS
    typedef struct ListaAdiacenza lista;
    typedef lista *PuntLista; //Assegnazione dell'alias PuntLista a ListaAdiacenza *
    typedef struct ListaNodi nodi;
    typedef nodi *PuntNodi; //Assegnazione dell'alias PuntNodi a ListaNodi *
    ho l'errore "invalid application of `sizeof' to an incomplete type" alla riga:

    PuntLista PuntNuovo;
    PuntNuovo = (PuntLista) malloc(sizeof(lista)); //Chiede spazio in memoria
    Il typedef corrente dovrebbe star bene in quanto facendo una cosa in un altro programma in C tutto va come dovrebbe.

  6. #6
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Fai attenzione quando scrivi i sorgenti ... in questa riga

    struct ListaAdicenza{//struttura per gli elementi della lista di adiacenza

    hai scritto

    ListaAdicenza

    e non

    ListaAdiacenza
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  7. #7
    Utente di HTML.it
    Registrato dal
    Aug 2009
    Messaggi
    7
    Che sbadato!

    Grazie 1000 di tutto!

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.