I don’t understand passing arrays to functions in C

The program I have works. Once I try to clean it up with functions I get totally lost. Can someone please review my code and help me understand what I'm doing wrong?

The program output needs to look like this (please note, this photo was taken prior to adding functions):

prefunction

Ideally, after the update_level loop, I would then print the totals.

Below is my code with functions that I don't understand at all.

#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,0,0,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


Read more here: https://stackoverflow.com/questions/64190267/i-dont-understand-passing-arrays-to-functions-in-c

Content Attribution

This content was originally published by Trevor at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: