strtok Unhandled exception;Access violation writin

2019-03-04 09:37发布

问题:

#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 = 0;
        do {
            random = (rand() % 20);
            list[i] = words[random];
            d = 0;
            for (j = 0; j < i; j++) {
                if (strcmp(words[random], list[j]) == 0)
                    d = 1;
            }
        } while (d);
    }

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

    while (token != NULL) {
        length = strlen(words[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;
}

Basically this program generates 4 random words which cannot be duplicate strtok is used to slip the words so that they can be entered char by char into matrix. finally any null chars in the matrix will be replaced with random characters. however strtok is generating runtime error and I am not sure how to check for a null element ?

回答1:

In this statement

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

function strtok tries to change a string literal addressed by the array element words[random].

String literals are immutable in C. Any attempt to change a string literal results in undefined behaviour.

Instead of the array of pointers to string literals

 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 "};

you should define a two dimensional character array initialized by the string literals. For example

 char words[20][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 "};


回答2:

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

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

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

      matrix [i][k]=token;

        token = strtok(NULL, search); 


          matrix [i][k]=token;

    } 
  }

This is messed and not what you intend. Change it to something straight, e. g.:

    for (k = 0, c = words[random]; token = strtok(c, search); c = NULL, ++k)
        matrix[i][k] = token;