如何将函数输出(数组)分配给c中的另一个函数?

时间:2015-12-10 21:30:04

标签: c arrays function pointers

我尝试对Hangman游戏进行编程,但我无法让它工作。编译工作,但当我执行此程序时,它不显示用于猜测字母的帮助数组... =(

游戏还没有结束,因为我无法让它发挥作用。

我尝试将函数helpArray的数组helpArrayFunc()交给函数drawHelpArray(),这样它就可以显示要猜的字母的帮助数组,但它没有做任何事情

这里有什么错误?

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

//--- Function Prototypes ---
int * helpArrayFunc(char word[19][20],char,int *,int *,int *,int *);
void drawHelpArray(char word[19][20],int *,int *,int *);
void drawHangMan(int *);
char letterFunc();

int main()
{   
    // Wörter werden initialisiert
    char word[19][20]={"Portal 2","Half-Life 2","Left 4 Dead 2","CS:GO","Rainbow Six","Fallout 4","Bioshock","Call of Duty","DayZ","H1Z1","Outlast","The Stanley Parable","Insurgency","Dying Light","Thief","Assasin's Creed","Wolfenstein","Payday 2","Need For Speed"};  
    // Zufallswort
    int numWord=0;
    srand(time(NULL));
    numWord=rand() % 19;
    // Wortlänge
    int wordLength = strlen(word[numWord]);
    // Fehler
    int mistakes=0;
    // HilfsArray
    int helpArray[wordLength];
    helpArray[wordLength]=0;
    // Buchstabe
    char letter;

    //printf("%s\n\n",word[numWord]); Only for testing

    do
    {
        drawHangMan(&mistakes);
        *helpArray=helpArrayFunc(word,letter,helpArray,&mistakes,&numWord,&wordLength);
        drawHelpArray(word,helpArray,&numWord,&wordLength);
        letter=letterFunc();
        system("cls");
    }
    while(mistakes<11);

    printf("\n\n___________________________\n\n");
    printf(" Copyright\xA9 Thomas Sapelza\n");
    printf("___________________________\n\n");
    system("PAUSE");
    return 0;
}

int * helpArrayFunc(char word[19][20],char letter,int *mistakes,int *helpArray,int *numWord,int *wordLength)
{
    bool mistake=false;

    // Buchstaben überprüfen ob er im Wort enthalten ist
    for(int i=0;i<(*wordLength);i++)
    {
        if(letter==word[*numWord][i])
        {   
            helpArray[i]=1;
            return helpArray;
        }
        else
        {
            helpArray[i]=0;
            mistake=true;
        }
    }

    // Auf fehler überprüfen
    if(mistake) *mistakes++;
}

void drawHelpArray(char word[19][20],int *helpArray,int *numWord,int *wordLength)
{
    // Zeichen die im HilfsArray immer angezeigt werden sollen
    char space='\x20',dash='\x2D',comma='\x27',colon='\x3A';

    // HilfsArray darstellen
    for(int i=0;i<(*wordLength);i++)
    {
        // Wenn der Wert von helpArray auf 1 ist wird der Buchstabe angezeigt
        if(helpArray[i]==1)
        {
            printf("%c ",word[*numWord][i]);
        }

        // Wenn der Wert von helpArray auf 0 ist wird ein Zeichen dargestellt
        if(helpArray[i]==0)
        {
            // Zeichen die immer enthalten sein sollen bzw. 
            if(word[*numWord][i]==space)
            {
                printf("  ");
            }
            else if(word[*numWord][i]==dash)
            {
                printf("- ");
            }
            else if(word[*numWord][i]==comma)
            {
                printf("' ");
            }
            else if(word[*numWord][i]==colon)
            {
                printf(": ");
            }
            else
            {
                printf("_ ");
            }
        }
    }
    printf("\n\n");
}

char letterFunc() // Buchstabe einlesen
{
    char letter;
    printf("\nGeben Sie ein Buchstabe ein: ");
    fflush(stdin);
    scanf("%c",&letter);
    return letter;
}

void drawHangMan(int *mistakes)
{

switch(*mistakes) 
    {
    case 0:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("\n");
        printf("\n");
        printf("\n");
        printf("\n");
        printf("\n");
        printf("\n");
        printf("____________\n\n");
    break;
    case 1:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 2:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 3:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 4:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 5:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |    |\n");
        printf("  |    |\n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 6:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |   \\|\n");
        printf("  |    | \n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 7:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |   \\|/\n");
        printf("  |    | \n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 8:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |   \\|/\n");
        printf("  |    | \n");
        printf("  |   /\n");
        printf("__|_________\n\n");
    break;
    case 9:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |   \\|/\n");
        printf("  |    | \n");
        printf("  |   / \\\n");
        printf("__|_________\n\n");
    break;
    case 10:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    X \n");
        printf("  |   \\|/\n");
        printf("  |    | \n");
        printf("  |   / \\\n");
        printf("__|_________\n\n");
    break;
    }
}

1 个答案:

答案 0 :(得分:0)

  

如何将函数输出(数组)分配给c?

中的另一个函数

C函数无法输出(返回)数组。此外,在START_NOT_STICKY中,START_STICKY不是数组,而是指向helpArrayFunc()的指针。但是,您不需要该函数来返回数组,因为它在helpArray的原始int上运行 - 它通过参数int helpArray[wordLength]对数组内容所做的每次更改引用原始数组,因此您可以在main()中删除错误的赋值int *helpArray(引用第一个数组元素)并声明为*helpArray=。因此,main()会收到原始数组地址,其值与void helpArrayFunc(…)相同 - 如果它们仍然存在错误,则会drawHelpArray()发生错误。
另请注意,您混淆了调用中helpArrayFunc()helpArrayFunc()的顺序以及helpArray的定义。