我不明白将数组传递给C中的函数

问题描述

我的程序有效。一旦尝试使用功能对其进行清理,我就会完全迷失方向。有人可以查看我的代码并帮助我了解我在做什么错吗?

程序输出应如下所示(请注意,这张照片是在添加功能之前拍摄的):

prefunction

理想情况下,在update_level循环之后,我将打印总计。

下面是我的代码,其中包含我根本不了解的功能

#include <stdio.h>

int update_level(int player[],int healthpoint);
int display_levels();

int main(void)
{
    int levels[6] = {1,2,3,4,5,6};  // levels is an array of 6 integers
    int players[6] = {0,0}; // players is an array of 6 integers
    int healthpoints;           // initialize variable
    
    
    printf("Enter total player health points (-1 to quit): ");  // user input
    scanf("%d",&healthpoints);         // reads user input
    
    for(j=0; j<=5; j++)
    {
        update_level(players[j],healthpoints);
    }
    
    return;
}   // end main

int update_level(int player[],int healthpoint);
{
    while (healthpoints != -1)
    {
        if ( healthpoints <= 9)
        {
            players[0] += 1;
        } //end if
        else if (healthpoints <= 19)
        {
            players[1] += 1;
        } //end else if
        else if (healthpoints <= 29)
        {
            players[2] += 1;
        } //end else if
        else if (healthpoints <= 39)
        {
            players[3] += 1;
        } //end else if
        else if (healthpoints <= 49)
        {
            players[4] += 1;
        } //end else if
        else
        {
            players[5] += 1;
        } // end else

        printf("Enter total player health points (-1 to quit): ");  // user input
        scanf("%d",&healthpoints);         // reads user input
        
    }   // end while
    
    return;
}   // end update_level


int display_levels()
{   
    int i;      // initialize variable
    
    printf("T O T A L S\n\n");
    
    for (i = 0; i < 6; ++i){
        printf("Level %u%13d\n",levels[i],players[i]);
    }       // end for
    
    return;
} // end display_levels

解决方法

在一些帮助下,我得以调试程序。下面是正确的代码。


#include <stdio.h>

int update_level(int players[],int healthpoints);
int display_levels(int levels[],int players[]);

int main(void)
{
    int levels[6] = {1,2,3,4,5,6};  // levels is an array of 6 integers
    int players[6] = {0,0}; // players is an array of 6 integers
    int healthpoints;           // initialize variable
    
    
    printf("Enter total player health points (-1 to quit): ");  // user input
    scanf("%d",&healthpoints);         // reads user input
    

    update_level(players,healthpoints);
    display_levels(levels,players);

    
    return;
}   // end main

int update_level(int players[],int healthpoints)
{
    while (healthpoints != -1)
    {
        if ( healthpoints <= 9)
        {
            players[0] += 1;
        } //end if
        else if (healthpoints <= 19)
        {
            players[1] += 1;
        } //end else if
        else if (healthpoints <= 29)
        {
            players[2] += 1;
        } //end else if
        else if (healthpoints <= 39)
        {
            players[3] += 1;
        } //end else if
        else if (healthpoints <= 49)
        {
            players[4] += 1;
        } //end else if
        else
        {
            players[5] += 1;
        } // end else

        printf("Enter total player health points (-1 to quit): ");  // user input
        scanf("%d",&healthpoints);         // reads user input
        
    }   // end while
    
    return;
}   // end update_level


int display_levels(int levels[],int players[])
{   
    int i;      // initialize variable
    
    printf("T O T A L S\n\n");
    
    for (i = 0; i < 6; ++i){
        printf("Level %u%13d\n",levels[i],players[i]);
    }       // end for
    
    return;
} // end display_levels