无论出于何种原因,for
中的getLetters
循环都会导致崩溃。
我无法挑出其中的特定来源,也许有人可以提供帮助?
我想我可能错误地完成了我的指针。我该如何解决这个问题?
#include <stdio.h>
#include <string.h>
#include <time.h>
int main() {
FILE *ifp;
ifp = fopen("dictionary.txt", "r");
int* lValues[26];
int* lDist[26];
int* lUsed[26];
int*fRun = 0;
int dictLen;
int i;
int bestValue;
fscanf(ifp, "%d", &dictLen);
char bestWord[7];
char dictionary[dictLen][7];
char* letters[7];
char* userWord[7];
//reads the dictionary into the array
for (i = 0; i < dictLen; i++) {
fscanf(ifp, "%s", &dictionary[i]);
}
distribution();
values();
while (i != 2) {
getLetters();
printf("Welcome to the Scrabble Practice Program!\n");
printf("\nHere are your letters: %s \n", letters);
printf("\nWhat would you like to do?\n");
printf("\t1-Enter Word\n");
printf("\t2-Quit\n\n");
printf("User Selection:");
scanf("%d", &i);
}
//fclose(ifp); not sure why, but this crashes the program
return 0;
}
//Gets the user's set of letters
void getLetters() {
char* letters[7];
int* lDist[26];
int* lUsed[26];
int lCur;
int i;
srand(time(0));
for (i = 0; i < 7; i++) {
*letters[i] = 65 + rand()%26;
lCur = (int)*letters[i] - 65;
while (*lUsed[lCur] >= *lDist[lCur]){
*letters[i] = 65 + rand()%26;
lCur = (int)*letters[i] - 65;
}
*lUsed[lCur]++;
}
}
//Sets the number of each letter available for distribution
void distribution() {
int* lDist;
//Number for A
lDist[0] = 9;
//Number for B
lDist[1] = 2;
//Number for C
lDist[2] = 2;
//Number for D
lDist[3] = 4;
//Number for E
lDist[4] = 12;
//Number for F
lDist[5] = 2;
//Number for G
lDist[6] = 3;
//Number for H
lDist[7] = 2;
//Number for I
lDist[8] = 9;
//Number for J
lDist[9] = 1;
//Number for K
lDist[10] = 1;
//Number for L
lDist[11] = 4;
//Number for M
lDist[12] = 2;
//Number for N
lDist[13] = 6;
//Number for O
lDist[14] = 8;
//Number for P
lDist[15] = 2;
//Number for Q
lDist[16] = 1;
//Number for R
lDist[17] = 6;
//Number for S
lDist[18] = 4;
//Number for T
lDist[19] = 6;
//Number for U
lDist[20] = 4;
//Number for V
lDist[21] = 2;
//Number for W
lDist[22] = 2;
//Number for X
lDist[23] = 1;
//Number for Y
lDist[24] = 2;
//Number for Z
lDist[25] = 1;
}
//Sets the value of all of the letters
void values() {
int* lValues;
//Value of A
lValues[0] = 1;
//Value of B
lValues[1] = 3;
//Value of C
lValues[2] = 3;
//Value of D
lValues[3] = 2;
//Value of E
lValues[4] = 1;
//Value of F
lValues[5] = 4;
//Value of G
lValues[6] = 2;
//Value of H
lValues[7] = 4;
//Value of I
lValues[8] = 1;
//Value of J
lValues[9] = 8;
//Value of K
lValues[10] = 5;
//Value of L
lValues[11] = 1;
//Value of M
lValues[12] = 3;
//Value of N
lValues[13] = 1;
//Value of O
lValues[14] = 1;
//Value of P
lValues[15] = 3;
//Value of Q
lValues[16] = 10;
//Value of R
lValues[17] = 1;
//Value of S
lValues[18] = 1;
//Value of T
lValues[19] = 1;
//Value of U
lValues[20] = 1;
//Value of V
lValues[21] = 4;
//Value of W
lValues[22] = 4;
//Value of X
lValues[23] = 8;
//Value of Y
lValues[24] = 4;
//Value of Z
lValues[25] = 10;
}
答案 0 :(得分:3)
*letters[i] = 65 + rand()%26;
字母[i]指的是无意义的(未初始化的),您最终可以使用Access violation fault
。
进一步
char* letters[7];
int* lDist[26];
int* lUsed[26];
所有这些都是arrays of pointer
而不是基本类型的数组(即int,char等)。我想你可能想要
char letters[7];//Array of seven characters
int lDist[26];//Array of 26 ints
int lUsed[26];//Array of 26 ints
并开始将*letters[i]
简称为letters[i]
答案 1 :(得分:1)
letters
,lDist
和lUsed
是指针数组(未初始化)。为什么不使用数组呢?
#include <stdlib.h>
#include <time.h>
void getLetters(void)
{
char letters[7];
int lDist[26];
int lUsed[26];
int lCur;
int i;
srand(time(NULL));
for (i = 0; i < 7; i++) {
letters[i] = 65 + rand() % 26;
lCur = (int) letters[i] - 65;
while (lUsed[lCur] >= lDist[lCur]) {
letters[i] = 65 + rand() % 26;
lCur = (int) letters[i] - 65;
}
lUsed[lCur]++;
}
}
答案 2 :(得分:1)
getLetters
函数中的每个单一数组访问都可能导致问题:您声明了三个从未初始化的指针数组,因此每次取消引用任何数组的元素时都会取消引用垃圾内存地址,有时试图写入该内存地址。
从您的代码中不清楚为什么需要指针数组。
为什么不简单:
char letters[7];
int lDist[26];
int lUsed[26];
摆脱解除引用?
但即使这样,该功能也存在问题。您需要初始化数组值,至少lDist
和lUsed
。您可以与lDist
的元素进行比较,但不会更改这些值。
答案 3 :(得分:1)
上述代码似乎没有按照您的意图执行。声明变量
时char* letters[7];
...你宣布一个7 char *
s的数组,而不是7 char
s。因此,当你说
*letters[i] = 65 + rand()%26;
...您首先索引数组并检索索引i
处的指针。然后取消引用该指针并尝试在那里存储一个字符。问题是letters
永远不会被初始化,并且最有可能充满垃圾:对于任何值i
,letters[i]
都会返回一个随机值。因此,当您尝试将其解除引用作为地址时,您将索引到其他人的内存中,并且写入它将导致分段错误。
您似乎想要声明letters
(以及lDist
和lUsed
),如下所示:
char letters[7];
int lDist[26];
int lUsed[26];
这意味着一个长度为7的char
数组。然后,您将在不解除引用的情况下解决它们:
letters[i] = 65 + rand()%26;
你也可以做以上......
*(letters + i) = 65 + rand()%26;
...但你通常不是要将两者混合在一起。
答案 4 :(得分:0)
不需要使用指针,使用普通数组就行了,就像这样(我还为lUsed和lDist添加了初始化):
void getLetters() {
char letters[7];
int lDist[26];
int lUsed[26];
int lCur;
int i;
srand(time(0));
// initialize the arrays
for (i=0;i<26;i++) {
lUsed[i] = lDist[i] = 0;
}
for (i = 0; i < 7; i++) {
letters[i] = 65 + rand()%26;
lCur = (int)letters[i] - 65;
while (lUsed[lCur] >= lDist[lCur]){
letters[i] = 65 + rand()%26;
lCur = (int)letters[i] - 65;
}
lUsed[lCur]++;
}
}