简体   繁体   English

我的 c 程序一直崩溃,我不知道为什么

[英]My c program keeps crashing and I do not know why

I am writing a program like hangman in c and I am trying to run it.The problem is that it's working fine until I give it a letter to quess the word but then it crashes with -1073741819 (0xC0000005).我在 c 中写了一个像刽子手这样的程序,我正在尝试运行它。问题是它工作正常,直到我给它一封信来查询这个词,然后它崩溃并显示 -1073741819 (0xC0000005)。 Can someone help me solve this, I think its something really small that I cant see.有人可以帮我解决这个问题吗,我认为它真的很小,我看不到。 Thank you for helping me!感谢你们对我的帮助!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Rules(void);
void maskWord (char starword[], int size);
int playRound(char starword[], char answer[]);
void updateStarWord(char starword[], char answer[], char userguess);
int occurancesInWord(char userguess, char answer[]);

int c=0;
char answer[128];
int N;

int main()
{
    int ch,size;
    char starword[1000];
    do
    {
        printf("---Welcome to Hangman!---\n");
        printf("1.Start Game\n2.Instructions\n3.Exit\n");
        scanf("%d",&ch);
        if(ch>0&& ch<4)
        {
            switch(ch)
            {
                case 1:
                    maskWord(starword,size);break;
                case 2:
                    Rules();break;
            }
        }
        else
            system("cls");
    }
    while(ch!=3);
    return 0;
}

void Rules(void)
{
    do
    {
        do
        {
            printf("\nThe word to guess is represented by a row of dashes representing each letter of the word.");
            printf("\nRules may permit or forbid proper nouns, such as names, places, brands, or slang.");
            printf("\nIf the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions.");
            printf("\nIf the suggested letter does not occur in the word, the other player draws one element of a hanged stick figure as a tally mark.\n");
        }
        while(getchar()!='\n');
    }
    while(getchar()!='\n');
}

void maskWord (char starword[], int size)
{
    printf("Enter word to guess: ");
    fflush(stdout);
    scanf(" %s", answer);
    int N = strlen(answer);
    int mask[N];
    for (int i=0; i < N; ++i)
    {
        mask[i] = 0;
    }
    playRound(mask,N);
}

int playRound(char starword[], char answer[])
{
    // Loop over each round of guessing
    int gameover = 0;
    while (! gameover)
    {
        // Print word with *s for unguessed letters
        printf("The word is : ");
        for(int j=0; j < answer; ++j)
        {
            if (starword[j])
            {
                printf("%c", answer[j]);
            }
            else
            {
                printf("*");
            }
        }
        printf("\n");
        // Get player's next guess
        char guess;
        printf("\nGive a letter: ");
        fflush(stdout);
        scanf(" %c", &guess);
        updateStarWord(starword,answer,guess);
    }

}

void updateStarWord(char starword[], char answer[], char userguess)
{
    // Mark true all mask positions corresponding to guess
    int k;
    for(k=0; k < answer; ++k)
    {
        if ((answer[k]) ==(userguess))
        {
            starword[k] = 1;
        }
    }
}

Your for loop doesn't make sense because the condition for terminating it is k < answer .您的 for 循环没有意义,因为终止它的条件是k < answer You are comparing an integer ( k ) to a pointer ( answer ).您正在将 integer ( k ) 与指针 ( answer ) 进行比较。 The compiler should have warned you about this, so make sure your compiler warnings are turned on and you are paying attention to them.编译器应该已经就此警告过您,因此请确保您的编译器警告已打开并且您正在关注它们。 Pointers and integers are different things, and comparing them is almost never what you want to do.指针和整数是不同的东西,比较它们几乎不是你想要做的。

If answer is null-terminated, you could probably replace that condition with answer[k] .如果answer以 null 结尾,您可能可以用answer[k]替换该条件。 Or maybe updateStarWord needs to take an argument that indicates the length of answer , and then the condition would be k < answer_length .或者updateStarWord可能需要采用一个参数来指示answer的长度,然后条件将是k < answer_length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM