C程序在do while循环中崩溃

C程序在do while循环中崩溃,c,pointers,C,Pointers,我正试图创建一个刽子手游戏,但当它进入for循环时,我的程序不断崩溃。我还在读指针的书,还没有完全理解它们。我知道你不能修改字符串文字,所以我试着制作可修改的数组,但我很确定崩溃与此有关。有人能指出车祸的具体原因吗 #include <stdio.h> #include <stdlib.h> #include <string.h> void main(int argc, char **argv) { char word[15]; char *word

我正试图创建一个刽子手游戏,但当它进入for循环时,我的程序不断崩溃。我还在读指针的书,还没有完全理解它们。我知道你不能修改字符串文字,所以我试着制作可修改的数组,但我很确定崩溃与此有关。有人能指出车祸的具体原因吗

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

void main(int argc, char **argv)
{
  char word[15];
  char *wordState = malloc(15);
  char *guess[1];
  int guesses = 6;
  int i;
  printf("Enter word.\0");
  scanf("%s",word);
  strcpy(wordState, "---------------");
  do{
    printf("Please guess a letter.\0");
    printf("You have %d\n guesses.\0", guesses);
    scanf("%s\n",guess);
    printf("%s\n", wordState);
    for(i=0; word[i] != '\0'; i++)
      {
       if(*guess[1]==word[i])
         {
           strcpy(*wordState[i],*guess[1]);
         }
       else
         {
           guesses--;
         }
      }
    }while(guesses != 0);

  free(wordState);
#包括
#包括
#包括
void main(整型argc,字符**argv)
{
字符字[15];
char*wordState=malloc(15);
char*guess[1];
整数猜测=6;
int i;
printf(“输入单词。\0”);
scanf(“%s”,单词);
strcpy(wordState,“--------------”;
做{
printf(“请猜一个字母。\0”);
printf(“您有%d\n个猜测。\0”,猜测);
scanf(“%s\n”,猜测);
printf(“%s\n”,wordState);
for(i=0;单词[i]!='\0';i++)
{
if(*猜测[1]==单词[i])
{
strcpy(*wordState[i],*guess[1]);
}
其他的
{
猜测--;
}
}
}而(猜测!=0);
免费(wordState);

大多数情况下,您的问题是

strcpy(wordState, "---------------");
因为您没有足够的空间来复制
\0
,从而导致内存溢出

同样,对于像
char*guess[1];
这样的定义,使用

if(*guess[1]==word[i])
是,因为

  • C
    数组是基于
    0
    的。[想想:你为什么要使用单元素数组?]
  • 您正在使用未初始化的内存
  • 我还注意到一些其他问题,比如

  • void main(int-argc,char**argv)
    应该是
    int-main(int-argc,char**argv)
  • 您没有检查
    malloc()
  • scanf(“%s”,word);
    应为
    scanf(“%14s”,word);
    以避免较长的输入可能导致缓冲区溢出

  • 你的问题主要是因为

    strcpy(wordState, "---------------");
    
    因为您没有足够的空间来复制
    \0
    ,从而导致内存溢出

    同样,对于像
    char*guess[1];
    这样的定义,使用

    if(*guess[1]==word[i])
    
    是,因为

  • C
    数组是基于
    0
    的。[想想:你为什么要使用单元素数组?]
  • 您正在使用未初始化的内存
  • 我还注意到一些其他问题,比如

  • void main(int-argc,char**argv)
    应该是
    int-main(int-argc,char**argv)
  • 您没有检查
    malloc()
  • scanf(“%s”,word);
    应为
    scanf(“%14s”,word);
    以避免较长的输入可能导致缓冲区溢出

  • 简化猜测,处理多次看到的信件

    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <stdbool.h> 
    
    void main(int argc, char **argv) 
    { 
        char word[16]; 
        char *wordState = malloc(16 * sizeof(char)); 
        char guess; 
        int guesses = 6; 
        printf("Enter word.\n"); 
        scanf("%s",word); 
        strcpy(wordState, "---------------"); 
        do{ 
            bool found = false; 
            printf("Please guess a letter.\n"); 
            printf("You have %d guesses.\n", guesses); 
            scanf(" %c", &guess); 
            for(int i=0; word[i] != '\0'; i++) 
            { 
                if(word[i] == guess) 
                { 
                    wordState[i] = guess; 
                    found = true; 
                } 
            } 
            if (!found) 
            { 
                guesses--; 
            } 
            printf("%s\n", wordState); 
        } while(guesses != 0); 
    
        free(wordState); 
    }   
    
    #包括
    #包括
    #包括
    #包括
    void main(整型argc,字符**argv)
    { 
    字符字[16];
    char*wordState=malloc(16*sizeof(char));
    猜字符;
    整数猜测=6;
    printf(“输入单词。\n”);
    scanf(“%s”,单词);
    strcpy(wordState,“--------------”;
    做{
    bool-found=false;
    printf(“请猜一个字母。\n”);
    printf(“您有%d个猜测。\n”,猜测);
    scanf(“%c”和“&guess”);
    for(int i=0;word[i]!='\0';i++)
    { 
    if(单词[i]==猜测)
    { 
    wordState[i]=猜测;
    发现=真;
    } 
    } 
    如果(!找到)
    { 
    猜测--;
    } 
    printf(“%s\n”,wordState);
    }而(猜测!=0);
    免费(wordState);
    }   
    
    简化猜测,处理多次看到的信件

    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <stdbool.h> 
    
    void main(int argc, char **argv) 
    { 
        char word[16]; 
        char *wordState = malloc(16 * sizeof(char)); 
        char guess; 
        int guesses = 6; 
        printf("Enter word.\n"); 
        scanf("%s",word); 
        strcpy(wordState, "---------------"); 
        do{ 
            bool found = false; 
            printf("Please guess a letter.\n"); 
            printf("You have %d guesses.\n", guesses); 
            scanf(" %c", &guess); 
            for(int i=0; word[i] != '\0'; i++) 
            { 
                if(word[i] == guess) 
                { 
                    wordState[i] = guess; 
                    found = true; 
                } 
            } 
            if (!found) 
            { 
                guesses--; 
            } 
            printf("%s\n", wordState); 
        } while(guesses != 0); 
    
        free(wordState); 
    }   
    
    #包括
    #包括
    #包括
    #包括
    void main(整型argc,字符**argv)
    { 
    字符字[16];
    char*wordState=malloc(16*sizeof(char));
    猜字符;
    整数猜测=6;
    printf(“输入单词。\n”);
    scanf(“%s”,单词);
    strcpy(wordState,“--------------”;
    做{
    bool-found=false;
    printf(“请猜一个字母。\n”);
    printf(“您有%d个猜测。\n”,猜测);
    scanf(“%c”和“&guess”);
    for(int i=0;word[i]!='\0';i++)
    { 
    if(单词[i]==猜测)
    { 
    wordState[i]=猜测;
    发现=真;
    } 
    } 
    如果(!找到)
    { 
    猜测--;
    } 
    printf(“%s\n”,wordState);
    }而(猜测!=0);
    免费(wordState);
    }   
    
    您可能想要
    字符猜测
    (或
    字符猜测[1]
    )而不是
    字符*猜测[1]
    。此外,您需要确保在
    wordState
    中为终端空字符留出空间。分配的长度应允许在数组中多放一个字符。请尝试16。您的代码将无法编译。您不能使用
    *wordState[i]
    。在
    printf
    s中不需要
    \0
    。编译此代码时,C编译器是否会发出一些警告?除非存在节省内存的主要原因(例如,您正在为带有rstricted RAM的嵌入式系统开发软件),将数组大小声明为不适当的大小是没有意义的。如果您正在处理文本行,请使用256,因为您不太可能找到更长的文本行。将字符数组大小调整为(例如)15会带来麻烦。您可能需要
    字符猜测
    (或
    字符猜测[1]
    )而不是
    字符*猜测[1]
    。此外,您需要确保在
    wordState
    中为终端空字符留出空间。分配的长度应允许在数组中多放一个字符。请尝试16。您的代码将无法编译。您不能使用
    *wordState[i]
    。在
    printf
    s中不需要
    \0
    。编译此代码时,C编译器是否会发出一些警告?除非存在节省内存的主要原因(例如,您正在为带有rstricted RAM的嵌入式系统开发软件),将数组大小声明为不适当的大小是没有意义的。如果要处理文本行,请使用256,因为它与