Visualizzazione dei risultati da 1 a 5 su 5

Discussione: [C] Problema puntatori

  1. #1

    [C] Problema puntatori

    Salve. Devo creare un programma che dato un testo, deve restituire la parola più lunga e la sua lunghezza, ma ho un problema di puntatori. Praticamente, al printf viene stampato: (null) e la lunghezza... ma perchè longestWord diventa null? Non capisco o_O Vorrei evitare di usare variabili globali...

    codice:
    #include <stdio.h>
    #include <string.h>
    
    void getMaxLengthWord (char *text, char *longestWord, int *maxLength);
    
    int main()
    {
        char text[] = "rosso verde blu";
        char *longestWord;
        int maxLength;
    
        getMaxLengthWord (text, longestWord, &maxLength);
    
        printf ("%s %d\n", longestWord, maxLength);
    
        return 0;
    }
    
    void getMaxLengthWord (char *text, char *longestWord, int *maxLength)
    {
        int counter = 0;
        *maxLength = 0;
    
        char *word = strtok (text, " ");
        while (word != NULL)
        {
            int length = strlen (word);
    
            if (length > *maxLength)
            {
                *maxLength = length;
                longestWord = word;
            }
    
            word = strtok (NULL, " ");
        }
    }

  2. #2
    Ho risolto facendo restituire un puntatore char alla funzione

    Grazie mille lo stesso, potete chiudere

  3. #3
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,472
    Non puoi modificare un parametro passato se non lo passi con un puntat.
    Anche il puntatore, a sua volta, deve essere passato tramite puntatore.

    In rosso le modifiche (mancano anche alcune inizializzazioni importanti ...).

    codice:
    void getMaxLengthWord (char *text, char **longestWord, int *maxLength);
    
    int main()
    {
        char text[] = "rosso verde blu";
        char *longestWord = NULL;
        int maxLength = 0;
    
        getMaxLengthWord (text, &longestWord, &maxLength);
    
        printf ("%s %d\n", longestWord, maxLength);
    
        return 0;
    }
    
    void getMaxLengthWord (char *text, char **longestWord, int *maxLength)
    {
        int counter = 0;
        *maxLength = 0;
    
        char *word = strtok (text, " ");
        while (word != NULL)
        {
            int length = strlen (word);
    
            if (length > *maxLength)
            {
                *maxLength = length;
                [color=red]*longestWord = word;
            }
    
            word = strtok (NULL, " ");
        }
    }
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  4. #4
    Originariamente inviato da oregon
    Non puoi modificare un parametro passato se non lo passi con un puntat.
    Anche il puntatore, a sua volta, deve essere passato tramite puntatore.

    In rosso le modifiche (mancano anche alcune inizializzazioni importanti ...).

    codice:
    void getMaxLengthWord (char *text, char **longestWord, int *maxLength);
    
    int main()
    {
        char text[] = "rosso verde blu";
        char *longestWord = NULL;
        int maxLength = 0;
    
        getMaxLengthWord (text, &longestWord, &maxLength);
    
        printf ("%s %d\n", longestWord, maxLength);
    
        return 0;
    }
    
    void getMaxLengthWord (char *text, char **longestWord, int *maxLength)
    {
        int counter = 0;
        *maxLength = 0;
    
        char *word = strtok (text, " ");
        while (word != NULL)
        {
            int length = strlen (word);
    
            if (length > *maxLength)
            {
                *maxLength = length;
                [color=red]*longestWord = word;
            }
    
            word = strtok (NULL, " ");
        }
    }
    Il metodo "puntatore di puntatore" non l'ho mai approvato, è troppo complicato! Preferisco la funzione che restituisce un puntatore

  5. #5
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,472
    Originariamente inviato da Dreamer89
    Il metodo "puntatore di puntatore" non l'ho mai approvato, è troppo complicato!
    Veramente mi sembra abbastanza semplice e utile in tanti casi ...
    No MP tecnici (non rispondo nemmeno!), usa il forum.

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.