Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11
  1. #1
    Utente di HTML.it L'avatar di Salvy95
    Registrato dal
    Jul 2008
    Messaggi
    199

    [C++] Problema puntatore

    Salve, ho un problema con questo codice

    codice:
    #include <iostream> #define MAXCHAR 10 using namespace std; char* getChar(); main(){     char* y;     y=getChar();     cout << *y <<" vale" <<y; system("\npause"); return 0; }   char* getChar(){     int x=0;     char* y=0;     char c[MAXCHAR]; while( cin.get()!= EOF){     c[x]=cin.get();         x++; }     y = &c[0];     return y; }
    Ho paura che l'errore stia nel cout, cioè sbaglio qui
    cout << *y <<" vale" <<y;
    ma come dovrei fare?

  2. #2
    Utente di HTML.it
    Registrato dal
    May 2008
    Messaggi
    475
    Problema di che tipo?
    Cosa vuoi fare?

    Copia il codice e poi mettici intorno i tag code a mano, se no è illeggibile.
    "Let him who has understanding reckon the number of the beast, for it is a human number.
    Its number is rw-rw-rw-."

  3. #3
    Utente di HTML.it L'avatar di Salvy95
    Registrato dal
    Jul 2008
    Messaggi
    199
    Si scusami hai ragione!
    codice:
    #include <iostream>
    #define MAXCHAR 10
    using namespace std;
    char* getChar();
    main(){
        char* y;
        y=getChar();
        cout << *y <<" vale" <<y; //Ho paura che il problema sia qui
    system("\npause");
    return 0;
    }
    
    
    char* getChar(){
        int x=0;
        char* y=0;
        char c[MAXCHAR];
    while( cin.get()!= EOF){
        c[x]=cin.get();
            x++;
    }
        y = &c[0];
        return y;
    }
    In pratica ho passato un array alla funzione utilizzando il puntatore, ma poi non riesco ad estrarre il contenuto della variabile a cui punta y (Il puntatore ritornato nella funzione getchar)

    Scusate la niubbiaggine

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    L'array c viene distrutto quando la funzione getChar ritorna, quindi y conterrà spazzatura. Se vuoi avere valori validi devi passare c alla funzione.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  5. #5
    Utente di HTML.it L'avatar di Salvy95
    Registrato dal
    Jul 2008
    Messaggi
    199
    Ma come si passa un array alla funzione?

  6. #6
    Utente di HTML.it
    Registrato dal
    Jul 2010
    Messaggi
    466
    Ma come si passa un array alla funzione?
    Per argomento..
    codice:
    /*dichiarazione funzione*/
    void foo(char bar[])
    {
      ...
    }
    int main()
    {
       char bar[N];
       ...
       foo(bar);
       ...
    }

  7. #7
    Utente di HTML.it
    Registrato dal
    Sep 2009
    Messaggi
    487

    ciao

    ciao salvy ho messo a posto un po' il codice ecco qui
    ricordati di mettere il '\0' finale alle stringhe
    codice:
    #include <iostream>
    #define MAXCHAR 10
    
    using namespace std;
    char* getChar();
    
    int main()
    {
    char* y;
    
    y = getChar();
    cout << "y = " << y; //Ho paura che il problema sia qui
    cout << "\n";
    
    system("pause"); // non si può fare system("\npause")...xD
    return 0;
    }
    
    
    char * getChar()
    {
    int x=0;
    static char c[MAXCHAR]; //dichiarando la variabile static lo stack relativo a getChar non viene distrutto
    c[0] = '\0';
    char buf;
    for(int i = 0; i < MAXCHAR - 1/*il -1 c'è xkè serve lo spazio per il NULL*/; i++) //fa in modo che il max numero di caratteri inseriti sia MAXCHAR
        {
        buf = cin.get();     
        if (buf == '\n') //fino a che premi invio il ciclo continua
            break;
        c[x] = buf;
        x++;
        }
    c[x] = '\0'; //va messo il '\0' a fine stringa se la crei te carattere per carattere
    cout << "c = " << c << endl;
    return c;
    }

  8. #8
    Utente di HTML.it L'avatar di Salvy95
    Registrato dal
    Jul 2008
    Messaggi
    199
    codice:
    #include <iostream>
    #define MAXCHAR 80
    using namespace std;
    char* funzione_inutile(int x,int y);
    main(){
    int x;
    int y;
    char* p;
    cout << "Inserisci due numeri\n\n";
    cin  >> x>>y;
    p=funzione_inutile(x,y);
    cout << "\n\n" << (char) p[4] <<"\n\n";
    system("pause");
    return 0;
    }
    
    char* funzione_inutile(int x,int y){
       static char c[MAXCHAR];
        int i;
        i=0;
        cout << "\n\nx+y= " << x+y << "\n\n Adessi inserisci un carattere\n\n\n";
         while(c[i]=getchar()!=EOF){
             i++;
         }
         c[i]='\0';
         return c;
    }
    Salve, ho modificato in questo modo.

    Ho aggiunto static, ho ritornato c che poi ho recuperato con il puntatore nella main...
    cosa sbaglio ancora?

    PS: ho cambiato il nome della funzione e aggiunto un piccolo programma per le addizioni, non badateci xD

  9. #9
    Utente di HTML.it
    Registrato dal
    Sep 2009
    Messaggi
    487

    ...

    non ha molto senso quello che hai scritto....
    1 perchè scrivi EOF??? come fai a inserire EOF da console (almeno io NON riesco...)
    invece di EOF metti '\n' //invio e prima del ciclo while scrivi anche fflush(stdin)...così azzera lo stream di input.

    2 perchè vuoi stampare a video (char) p[4]????????????????? non hai mai esplicitamente assegnato niente a p[4]! piuttosto stampa p senza il cast a char cout << p;

    3 ultimo consiglio, nell while metti while ( (c[i]=getchar()) != '\n' ) con le parentesi pr evitare di confonderti con le regole di precedenza associatività



    codice a posto:

    [CODE]
    #include <iostream>
    #define MAXCHAR 80
    using namespace std;
    char* funzione_inutile(int x,int y);
    main(){
    int x;
    int y;
    char* p;
    cout << "Inserisci due numeri\n\n";
    cin >> x >> y;
    p=funzione_inutile(x,y);
    cout << "\n\n" << p <<"\n\n";
    system("pause");
    return 0;
    }

    char* funzione_inutile(int x,int y){
    static char c[MAXCHAR];
    int i;
    i=0;
    cout << "\n\nx+y= " << x+y << "\n\n Adessi inserisci un carattere\n\n\n";
    fflush(stdin);
    while((c[i]=getchar()) != '\n')
    {
    i++;
    }
    c[i]='\0';
    return c;
    }

    [

  10. #10
    Utente di HTML.it
    Registrato dal
    Sep 2009
    Messaggi
    487

    ...

    non ha molto senso quello che hai scritto....
    1 perchè scrivi EOF??? come fai a inserire EOF da console (almeno io NON riesco...)
    invece di EOF metti '\n' //invio e prima del ciclo while scrivi anche fflush(stdin)...così azzera lo stream di input.

    2 perchè vuoi stampare a video (char) p[4]????????????????? non hai mai esplicitamente assegnato niente a p[4]! piuttosto stampa p senza il cast a char cout << p;

    3 ultimo consiglio, nell while metti while ( (c[i]=getchar()) != '\n' ) con le parentesi pr evitare di confonderti con le regole di precedenza associatività



    codice a posto:

    codice:
    #include <iostream>
    #define MAXCHAR 80
    using namespace std;
    char* funzione_inutile(int x,int y);
    main(){
    int x;
    int y;
    char* p;
    cout << "Inserisci due numeri\n\n";
    cin  >> x >> y;
    p=funzione_inutile(x,y);
    cout << "\n\n" << p <<"\n\n";
    system("pause");
    return 0;
    }
    
    char* funzione_inutile(int x,int y){
       static char c[MAXCHAR];
        int i;
        i=0;
        cout << "\n\nx+y= " << x+y << "\n\n Adessi inserisci un carattere\n\n\n";
        fflush(stdin);
        while((c[i]=getchar()) != '\n')
        {
        i++;
        }
         c[i]='\0';
         return c;
    }

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 © 2025 vBulletin Solutions, Inc. All rights reserved.