生成strtok错误::如何修复

时间:2015-12-02 08:36:32

标签: c exception-handling strtok runtimeexception unhandled-exception

#include <stdio.h>
#include <time.h>
#include <string.h>
char* matrix [10][10];  
int main(void){         
        int i;
          char* list[4];
         char* words[][20]={" c a t "," c a r "," b e a r "," s h i p "," m o u s e "," b e a t l e "," c o a t "," n e s t "," i c e "," s u g a r "," b a c o n "," f r o w n "," s m i l e "," d e a d "," f e a t h e r "," g o a t "," h e n "," j e l l y "," k o a l a "," l i p s "};

        int length;
        int num;
        int k;
        int m;
        char otherString=0;
        char* c;
        int j;
        int s;
        int r;


        char test[10];
        char* token;

const char *search = " ";
        char* empty="";
        int size;
        int ans;

        int x;
        int y;
        int pos;
        int pos2;
        int randRow;
        int randColumn;
        int chosen[10];
        int random;
        int d;
        int ROWS      = 10;      // number of rows
 int COLUMNS   = 10;      // number of columns




        printf("\tA\tB\tC\tD\tE\tF\tG\tH\tI\tJ\n");


        srand(time(NULL));

        for( i=0;i<4;i++){
    printf( "\n%d",i );
    d=0;
        do{
            random = (rand()%20);
            list[i]=words[0][random];
            d=0;
            for( j=0;j<i;j++){
                if(strcmp(words[0][random],list[j])==0)d=1;
            }
        }while(d);
        }


    token = strtok((words[0][random]),search); 

    while(token!=NULL) 
   {
      length=strlen(words[0][random]);

      for( k=0;k<length;k++){

        matrix [i][k]=token;

          token = strtok(NULL, search); 


            matrix [i][k]=token;

      } 
    } 


            for(r=0;r<10;r++){
            printf("\n");   
            for(s=0;s<10;s++){
            //ans=strlen(matrix[r][s]);
                /*if(ans==0){
                c= 'A' + (rand() % 26);
                matrix[r][s]=c;

            }*/
                 printf("\t%s",matrix[r][s]);


            }}


        getchar();
            return 0;
            }
  
    

基本上这是我的代码。它编译得很好但是strtok产生了一个     运行时异常。将修复我的错误感谢这个代码意味着选择随机的4个不重复的单词     strtok用于分隔它们将它们输入矩阵     即使回答,这仍然无法奏效。它不显示     正确的结果

  

0 个答案:

没有答案