C 从另一个函数输出数组

标签 c

我正在尝试从我的函数“enter”复制数组获胜者,以便我能够将其输出到“上一个”函数。当选择前一个选项的选项时,我没有得到任何输出。只有最后一个名为“previous”的函数不起作用,但要产生问题,需要大部分代码。

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

char enter(char names[][20]);
void menu();
void previous(char winner[][8]);

int main()
{
    char names[16][20];
    int i;

    printf("Please enter the names of the players:\n");

        /*Making the user enter 16 times*/
    for (i = 0; i < 16; i++) 
    {   
        scanf("%9s", &names[i]);
        fflush(stdin);
    }
    /*Clearing Screen*/
    system("cls");

    menu(names);
    return names[16][20];

}
void menu(char names[][20], char winner[][8])
{
    int choice;

    printf("Please select one of the following options:\n\n"
            "Press 1 to enter game  results\n"
            "Press 2 to display the current round\n"
            "Press 3 to display the players advancing to the next round\n"
            "Press 4 to display the previous round\n"
            "Press 5 to exit the program\n");

    scanf("%d", &choice);

    if(choice == 1)
    {
        enter(names);
    }

    system("cls");
    if(choice == 3)
    {
        previous(winner);
    }               
}
char enter(char names[][20])
{

    int result;
    int score1;
    int score2;
    int p, c, j, l, i;
    char winner[8][8];  
        system("cls");

        for(i = 0; i < 8; i++)
        {
            printf("\n\n%s vs %s",names[i],names[i+8]);

            score1 = 0;
            score2 = 0;

            for(j = 0; j < 5; j++)
            {
                printf("\n\nEnter game %d results, press 1 if %s won or"
                       " 2 if %s won :\n",(j+1), names[i], names[i+8]);
                scanf("%d", &result);

                if(result == 1)
                {
                    score1++;
                }
                if(result == 2)
                {
                    score2++;
                }

                printf("\n\n1Current score is %d-%d", score1, score2);

                if(score1 == 3)
                {
                    printf("\n\n%s adavances to the next round!",names[i]);
                    strncpy(winner[i], names[i], 10);
                    printf("\n\nPress Enter to Continue");
                    getch();
                    system("cls");
                    break;
                }

                if(score2 == 3)    
                {
                    printf("\n\n%s adavances to the next round!",names[i+8]);
                    strncpy(winner[i], names[i+8], 10);
                    printf("\n\nPress Enter to Continue");
                    getch();
                    system("cls");
                    break;
                }
            }
        }
        system("cls");
        printf("The players advancing to the next round are:\n\n");

        for(p = 0; p < 8; p++)
        {
            for(c = 0; c < 8; c++)
            {
                printf("%c",winner[p][c]);
            }
            printf("\n");
        }

        printf("\n\nPress Enter to Continue");
        getch();
        system("cls");
        menu(names, winner);
        return winner[8][8];            
}

void previous(char winner[][8])
{
    int i, j;

        for(i = 0; i < 8; i++)
        {
            for(j = 0; j < 8; j++)
            {
                printf("%c",winner[i][j]);
            }
            printf("\n");
        }
}     

最佳答案

您的程序中没有数组winner的数据!至少在您第一次调用它时不会。

菜单函数的签名是:

void menu(char names[][20], char winner[][8]);

但是你可以像这样从 main 调用它:

menu(names);

缺少winner参数。这不应该发生,但您已经声明了该函数的原型(prototype),即:

void menu();

不幸的是,C 将空括号视为“您传递的任何参数”,而不是不带参数的函数。这意味着你的函数调用会被忽略。修复方法是为原型(prototype)提供正确的签名,并从 main 传递合适的 winner 数组。

奇怪的是,您的 enter 函数提供了一个本地数组 winner。当您调用 enter 时,该数组将始终是一个新数组。这可能不是你想要的。按原样,您的程序应该有一个 names 和一个 winner 数组。 (您可以传递这些数组,但应该确保这些数组是一致的。当您确实想对现有数组进行操作时,不要创建新数组。)

您还可以递归地调用您的菜单。这意味着您会更深入地了解调用结构,但没有真正的好处。不要那样做;使用循环代替:do 显示菜单while 用户尚未选择“退出”。 (有一些递归函数的应用程序,但这不是一个。)

关于C 从另一个函数输出数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33889629/

相关文章:

c - 如何填充字符串可变大小数组?

c++ - 指针)是否有可能已经占据(ptr + 1)?

c - 我需要更好地解释 100 扇门计划

C:Read() 并使用换行符

计算系统调用次数

c - 为什么我不能打印这个指针的值?

python - python中实例变量和属性之间的区别?

c - 二进制补码运算

c - 获取安装在 solaris 11.2 机器上的卷列表

c++ - 数组索引循环变量在 C、C++ 中重置为零