0

无论出于何种原因,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;
}
4

5 回答 5

3
*letters[i] = 65 + rand()%26;

letter[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]

于 2012-09-13T16:59:56.187 回答
1

函数中的每一个数组访问getLetters都可能导致一个问题:您声明了三个从未初始化的指针数组,因此每当您取消引用任何数组的元素时,您都会取消引用垃圾内存地址,有时会尝试写入该地址内存地址。

从您的代码中不清楚为什么需要指针数组。

为什么不简单:

char letters[7];
int lDist[26];
int lUsed[26];

并摆脱取消引用?

但即便如此,该功能也存在问题。你需要初始化你的数组值,至少lDistlUsed. 您可以与 的元素进行比较,lDist但绝不会更改这些值。

于 2012-09-13T17:00:14.953 回答
1

上面的代码似乎没有按照您的意图进行。当你声明变量时

char* letters[7];

...您声明的是 7 char *s 的数组,而不是 7 chars。因此,当你说

*letters[i] = 65 + rand()%26;

...您首先索引数组并检索 index 处的指针i。然后,您取消引用该指针并尝试在其中存储一个字符。问题是letters从未初始化,并且很可能充满垃圾:对于任何 value iletters[i]将返回一个随机值。因此,当您尝试将其作为地址取消引用时,您正在索引其他人的内存,并且写入它会导致分段错误。

您似乎打算像这样声明letters(和lDistlUsed):

char letters[7];
int lDist[26];
int lUsed[26];

char这意味着一个长度为 7的 s 数组。然后您可以在不取消引用的情况下处理它们:

letters[i] = 65 + rand()%26;

您也可以按照以下方式执行上述操作...</p>

*(letters + i) = 65 + rand()%26;

......但你通常并不意味着将两者混合在一起。

于 2012-09-13T17:10:03.380 回答
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]++;
    }
}
于 2012-09-13T16:59:19.017 回答
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]++;

    }
}
于 2012-09-13T17:02:22.960 回答