From: Derek C. <de...@ci...> - 2009-03-06 00:29:21
|
You've just mixed in too much test code with the production stuff! As given, the tokenizar function works fine. However combine it with the main program given and the problem is that array[] in the main program is not initialised. Trying to dereference uninitialised pointers (in the printf) doesn't usually mean that good things happen! You have a general problem with tokenizar in that there is no way to get the tokens back to the main program. You might want to add another parameter or two, i.e. make the signature void tokenizar(char *palavra, char* array[], unsigned int size_of_array) Now you can pass in the token array and get results back. On Thu, Mar 5, 2009 at 10:49 PM, Alexsandro Meireles <mei...@gm...> wrote: > Here it is. > > #include<string.h> //É DEMAIS ESTE SCRIPT. DÁ PARA REFERENCIAR CADA SÍLABA > E CADA PALAVRA > #include<stdio.h> //Se a sílaba 1 for X e sílaba 2 for Y. Se a palavra 1 > for X e a palavra 2 for Y. > > void tokenizar(char *palavra) //void acentua(char *palavra) > { > //char search_string[]="Woody Norm Cliff"; > // char str[50]; //Que tamanho devo botar aqui > char *array[50]; > int loop; > > // gets(str); > > > array[0]=strtok(palavra," ,"); //Analisa para ver se tem algo escrito. O > primeiro string no caso tem que ser separado por um espaço vazio. > if(array[0]==NULL) > { > printf("No test to search.\n"); > exit(0); > } > > for(loop=1;loop<50;loop++) > { > array[loop]=strtok(NULL," ,.:!?"); //Separadores usados " " , : . > if(array[loop]==NULL) > break; > } > > for(loop=0;loop<50;loop++) > { > if(array[loop]==NULL) > break; > printf("Item #%d is %s\n",loop,array[loop]); //fazer um loop for > (;loop;) Sei lá. > } > system("pause"); > return; //tava return 0 > } > //fazer array[tok] = silaba A cada loop incrementa a silaba > > > //IMPORTANTE!!!!!!! TRANSFORMAR ISSO EM FUNÇÃO!!!! > > > > > On Thu, Mar 5, 2009 at 7:45 PM, Derek Clarke <de...@ci...> wrote: >> >> Can you tell us what's inside the tokenizar function? >> >> Also the code you have published doesn't match the results you report >> - you are obviously printing the words in a loop, so where's the loop >> in main? >> >> On Thu, Mar 5, 2009 at 11:36 AM, Alexsandro Meireles >> <mei...@gm...> 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 >> > >> > ------------------------------------------------------------------------------ >> > Open Source Business Conference (OSBC), March 24-25, 2009, San >> > Francisco, CA >> > -OSBC tackles the biggest issue in open source: Open Sourcing the >> > Enterprise >> > -Strategies to boost innovation and cut costs with open source >> > participation >> > -Receive a $600 discount off the registration fee with the source code: >> > SFAD >> > http://p.sf.net/sfu/XcvMzF8H >> > _______________________________________________ >> > Dev-cpp-users mailing list >> > Dev...@li... >> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >> > >> > > > > > -- > 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 > |