I decided to go over the C Primer Plus (6th) again to see if I get a better understanding.

I have seen a few things which I was not aware of. It's been kind of nice.

But when doing the exercise listed above, I noticed something strange. I had the struct you are supposed to display, printed from the `main()` function. It worked perfectly. I wanted to clean up the `main()` function and put the display struct code into a `print_roster` function.

For some reason the `print_roster()` function doesn't work right. I really can't understand it. Here is the code:

Code:
#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PROGRAM_NAME_SIZE   50
#define TOTAL_PLAYRERS      19

struct _roster {
    int number;
    char first[20];
    char last[20];
    int bats;
    int hits;
    int walks;
    int rbis;
    float average;
};

char program_name[PROGRAM_NAME_SIZE + 1] = {0};

void calculate_average (struct _roster *roster);
void print_roster(struct _roster *roster);

int main (int argc, char *argv[])
{
    struct _roster roster[TOTAL_PLAYRERS] = {
        {0, "Joe", "Shmoe", 3, 1, 0, 0},
        {4, "Shoeless", "Joe", 5, 1, 1, 1},
        {5, "Joey", "Makrs", 4, 1, 1, 0},
        {7, "Slim", "Jim", 1, 0, 0, 0},
        {18, "Jay", "Hay", 9, 2, 2, 2},
    };

    strcpy(program_name, basename(argv[0]));

    calculate_average(roster);
//    print_roster(roster);

/*
    char temp[5] = {0};

    puts(" #  First      Last        B   H   W  RBI   AVG");
    puts("==  =====      ====       ==  ==  ==  ===   ===");

    for(int x = 0; x < TOTAL_PLAYRERS; x++)
        if(roster[x].first[0] != '\0')
        {
            sprintf(temp, "%.3f", roster[x].average);
            printf("%2d  %-10s %-10s %2d  %2d  %2d   %2d  %s\n", roster[x].number, roster[x].first, roster[x].last, roster[x].bats, roster[x].hits, roster[x].walks, roster[x].rbis, &temp[1]);
        }
*/

    exit(EXIT_SUCCESS);
}

void calculate_average (struct _roster *roster)
{
    for(int x = 0; x < TOTAL_PLAYRERS; x++)
        if(roster[x].first[0] != '\0')
            roster[x].average = (float) roster[x].hits / (float) roster[x].bats;
}

void print_roster(struct _roster *roster)
{
    char temp[5] = {0};

    puts(" #  First      Last        B   H   W  RBI   AVG");
    puts("==  =====      ====       ==  ==  ==  ===   ===");

    for(int x = 0; x < TOTAL_PLAYRERS; x++)
        if(roster[x].first[0] != '\0')
        {
            sprintf(temp, "%.3f", roster[x].average);
            printf("%2d  %-10s %-10s %2d  %2d  %2d   %2d  %s\n", roster[x].number, roster[x].first, roster[x].last, roster[x].bats, roster[x].hits, roster[x].walks, roster[x].rbis, &temp[1]);
        }
}
The program has been minimized a bit by removing the reading of the roster file to populate the struct array. Basically, it you run the program as is, it displays the correct info:

Code:
 #  First      Last        B   H   W  RBI   AVG
==  =====      ====       ==  ==  ==  ===   ===
 0  Joe        Shmoe       3   1   0    0  .333
 4  Shoeless   Joe         5   1   1    1  .200
 5  Joey       Makrs       4   1   1    0  .250
 7  Slim       Jim         1   0   0    0  .000
18  Jay        Hay         9   2   2    2  .222
But if you comment out the display block of code, and uncomment the `print_roster(roster);` line, the program gets messed up and just keeps repeating the final line:

Code:
 #  First      Last        B   H   W  RBI   AVG
==  =====      ====       ==  ==  ==  ===   ===
 0  Joe        Shmoe       3   1   0    0  .333
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
 0  Joe        Shmoe       3   1   0    0  .200
It's like the for loop in the function stops working. I can't explain it. Can someone here help me understand what is happening?