0

I'm new to the world of C programming, and I as trying to code a primitive, terminal-based version of the "Hangman" game.

One of the steps doing this (or at least the way I am working on), is to create a second char array (next to the original char array that stores the word one needs to guess), filled with "*" for every Char of the original array, and display it. Although the rest of the programming part is not there yet (since I am not finished with it yet), I doubt it is relevant for now (however I allready know how to proceed, that is if I weren't bothered by some error-messages)....

Here's the code I have so far:

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

void hiddenArray(char array[]);

char *secretWord;
char *arrayCover;
char letter;

int main(int argc, char *argv[]){

secretWord = "Test";

printf("Welcome to the Hangman!\n\n");

hiddenArray(secretWord);
printf("What is the hidden Word?: %c\n", *arrayCover);  

printf("Guess a letter\n");
scanf("%c", &letter); 


}

void hiddenArray(char array[]){

int size,i;
size = sizeof(*array);

for (i=0;i<size-1;i++){
    *(arrayCover+i) = "*";
}
}

Now I have two issues... the first one: I don't understand the error message I am getting after compilation:

pendu.c:41:19: warning: incompatible pointer to integer conversion assigning to 'char' from 'char [2]' [-Wint-conversion]
            *(arrayCover+i) = "*";
                            ^ ~~~
1 warning generated.

And my second question: the second Array created, filled with "*" is not being displayed, what did I do wrong?

I'd love for some help, cheers!

4

4 回答 4

2

您的程序有一些错误需要纠正。

1)你*arraycover指向一些未知的值,你还没有初始化它。

2)sizeof(*array)应该是sizeof(array)

3)*(arrayCover+i) = "*"应该是*(arrayCover+i) = '*'

我建议您在不需要时不要创建太多全局变量

创建char secretWord[100] = "Test"而不是 char *secretWord

于 2013-11-10T18:32:03.373 回答
1

尝试这个

size = sizeof(arrayCover)/sizeof(char);

for (i=0;i<size-1;i++){
    *(array+i) = '*';
}

尽管如此,我认为你需要为arrayCover

arrayCover = malloc(sizeof(char) * number_of_elements);
于 2013-11-10T18:34:23.043 回答
0

"*"是一个字符串。改为使用'*'来获取字符。

于 2013-11-10T18:29:28.440 回答
0

"*"是一个字符串,它也包含\0,导致 2 个字符。而是使用'*'which 只是一个字符。

该函数具有需要在本地使用hiddenArray的形式参数,而不是如下所示,arrayarrayCover

void hiddenArray(char array[]){
   int size,i;
   size = sizeof(array)/sizeof(array[0]);
   for (i=0;i<size-1;i++){
         *(array+i) = '*'; /* Or array[i] = '*' */
   }
}
于 2013-11-10T18:29:40.903 回答