【问题标题】:strtok Unhandled exception;Access violation writing locationstrtok 未处理异常;访问冲突写入位置
【发布时间】:2015-12-01 21:55:20
【问题描述】:
#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;
}

基本上这个程序会生成 4 个随机词 复制 strtok 是用来溜字的,让他们 可以逐个字符地输入矩阵。最后任何 矩阵中的空字符将被随机替换 人物。 但是strtok 正在生成运行时错误,而我没有 确定如何检查空元素?

【问题讨论】:

  • strtok 更改字符串,但不能更改字符串文字。
  • 那么我该如何解决这个错误?
  • 如何检查空元素?
  • 猫是不是在你的空格键上乱走?

标签: c runtime-error strtok string-literals unhandled


【解决方案1】:

在此声明中

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

函数strtok 尝试更改由数组元素words[random] 寻址的字符串文字。

字符串文字在 C 中是不可变的。任何更改字符串文字的尝试都会导致未定义的行为。

而不是指向字符串字面量的指针数组

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

你应该定义一个由字符串字面量初始化的二维字符数组。例如

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

【讨论】:

  • 这不会产生错误,但仍然不是正确的结果?
  • @chris 更正数组的错误并提出一个新问题。更改问题的内容不是一个好主意。这让读者感到困惑。
【解决方案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;

    } 
  }

这是一团糟,不是你想要的。将其更改为直接的东西,例如。 g.:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2013-01-23
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多