strtok 未处理异常;访问冲突写入位置 [英] strtok Unhandled exception;Access violation writing location
问题描述
#include #include #include 字符*矩阵[10][10];int main(void) {国际我;字符*列表[4];字符*词[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 ug a r ","b a c o n ","f r o w n ","sm i l e ", "d e a d ", "f e t h e r ", "go a t ","h e n ","jel l y ","k o a l a ","l i p s "};整数长度;整数;国际 k;国际米;字符其他字符串=0;字符*c;国际j;整数;内部r;字符测试[10];字符 * 令牌;const char *search = " ";字符 *空 = "";整数大小;int ans;整数 x;输入 y;内部位置;int pos2;int randRow;int randColumn;int 选择 [10];整数随机;国际d;整数行 = 10;//行数整数列 = 10;//列数printf("\tA\tB\tC\tD\tE\tF\tG\tH\tI\tJ\n");srand(时间(空));for (i = 0; i <4; i++) {printf("\n");d = 0;做 {随机 = (rand() % 20);列表[i] = 单词[随机];d = 0;for (j = 0; j
<块引用>
基本上这个程序会生成 4 个随机单词,这些单词不能被复制strtok
用于滑动单词,以便它们可以逐个字符输入到矩阵中.最后任何矩阵中的空字符将被随机替换人物.但是 strtok
正在生成运行时错误,而我没有确定如何检查空元素?
在此声明中
token = strtok((words[random]),search);
function strtok
尝试更改由数组元素 words[random]
寻址的字符串文字.
字符串字面量在 C 中是不可变的.任何更改字符串字面量的尝试都会导致未定义的行为.
代替指向字符串文字的指针数组
char *words[20]={"cat","car","bear","ship","mouse","beatle","coat","nest","ice",糖"、培根"、皱眉"、微笑"、死"、羽毛"、山羊"、母鸡"、果冻"、考拉"、嘴唇"};
您应该定义一个由字符串文字初始化的二维字符数组.例如
char words[20][20]={"cat","car","bear","ship","mouse","beatle","coat","nest","ice"、"糖"、"培根"、"皱眉"、"微笑"、"死"、"羽毛"、"山羊"、"母鸡"、"果冻"、"考拉"、"嘴唇"};
#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. howeverstrtok
is generating runtime error and I am not sure how to check for a null element ?
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 "};
这篇关于strtok 未处理异常;访问冲突写入位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!