Salve a tutti. Ho creato un semplice programma che conta quante parole di un testo sono formate da tre lettere:
codice:
#include <stdio.h>
#include <string.h>
int getWordsNumberWith3Letters (char text[]);
int main()
{
char text[] = "tre tigri contro tre gatti";
int counter = getWordsNumberWith3Letters (text);
printf ("%d\n", counter);
return 0;
}
int getWordsNumberWith3Letters (char *text)
{
int counter = 0;
char *word = strtok (text, " ");
while (word != NULL)
{
if (strlen (word) == 3)
counter++;
word = strtok (NULL, " ");
}
return counter;
}
Il programma così com'è funziona correttamente, ma se provo a cambiare questo:
codice:
char text[] = "tre tigri contro tre gatti";
con questo:
codice:
char *text = "tre tigri contro tre gatti";
Avviando il programma mi compare il messaggio:"Segmentation Fault" :O
Perchè?