From: Alexsandro M. <mei...@gm...> - 2009-03-06 13:38:16
|
I corrected what you guys suggested to me. fgets intead of gets, input array as parameter, and so on. Thank you very much for your comments. Now the program is working a way better. On Fri, Mar 6, 2009 at 6:42 AM, Per Westermark <pw...@ia...> wrote: > 1) array is a private variable inside main(). If you want this array of > poinster to be initialized by a function call to tokenizar, then you must > send the array as a parameter, together with the information that it can > hold at most 50 words. > > And you must also let the function tokenizar return the number of words it > found, so you know how many words to print. > > But another thing. Do not ever (!) use gets() to read a string. Always use > fgets(). The reason is that gets() does not take the maximum size of the > input buffer as parameter, so if the input sentence is larger than 49 > characters, you will get a buffer overrun. > > If you use fgets(), it will end the input when the buffer is full. You can > then check if there is a '\n' (newline character) in the string, or if the > input string was too long. > > /pwm > > On Thu, 5 Mar 2009, Alexsandro Meireles wrote: > > > Dear all, > > > > I have build a function to tokenize a sentence and to return the words in > my > > main function. It seems ok, but it's not working the way I intend. > > > > The code is: > > > > main() > > { > > char str[50]; > > char *array[50]; > > printf("Token 0.1\nDeveloped by Alexsandro Meireles\nType"); > > printf(" the sentence: "); > > gets(str); > > tokenizar(str); > > printf("\n"); > > printf("Word #%d is %s\n",0, array[0]); //PROBLEM IS HERE!!!!! > > system("pause"); > > } > > > > > > I need to have access to the words returned by the function tokenizar, > but > > what I get as result, for example for "a casa pegou fogo." is: > > > > Word #0 is 'a' > > Word #1 is 'casa' > > Word #2 is 'pegou' > > Word #3 is 'fogo' > > > > AND THEN A LOOP > > > > Word #0 is Word #0 is Word #0 is Word #0 is ... > > > > IN OTHER WORDS, I CAN'T HAVE ACCESS TO THE ARRAY OF WORDS CREATED INSIDE > > 'tokenizar'. > > > > What am I doing wrong? > > > > Thanks in advance! > > > > -- > > Prof. Dr. Alexsandro Meireles, linguist > > Federal University of Espírito Santo > > Departamento de Línguas e Letras > > Av. Fernando Ferrari, 514. Campus Universitário > > Goiabeiras. 29075-910 > > Vitória-ES. Brazil > > mei...@gm... > > +55-27-41021734 > > > > -- Prof. Dr. Alexsandro Meireles, linguist Federal University of Espírito Santo Departamento de Línguas e Letras Av. Fernando Ferrari, 514. Campus Universitário Goiabeiras. 29075-910 Vitória-ES. Brazil mei...@gm... +55-27-41021734 |