0

To preface, I am new to coding, so if there is an entirely obvious issue here that I am missing, I apologise in advance.

I have a bit of code I'm working on where an assortment of names and numbers are read from a file into a struct seen below.

typedef struct KnightsBallLottoPlayer {
    char firstName[20];
    char lastName[20];
    int numbers[6];
} KBLottoPlayer;

I put the scanning of the file itself into its own void function like so, using pointer KBLottoPlayer * player; in main:

void scanning(KBLottoPlayer*players){
    int i, j;

    FILE * ifp = NULL;
    ifp=fopen("KnightsBall.in", "r");

    fscanf(ifp, "%d", &people);
    players = malloc(people * sizeof(KBLottoPlayer));

    for(i=0;i<people;i++){
        fscanf(ifp, "%s%s", &players[i].lastName, &players[i].firstName);

        for(j=0;j<6;j++){
            fscanf(ifp, "%d", &players[i].numbers[j]);
        }
    }
}

So far so good, I print out all of these values while in the function immediately afterwards, and they all line up just fine. No issues. They print accurate values and names. I return to main, and print them out again before doing anything else, and suddenly I'm getting random ASCII characters for names and values like -32342342 for numbers.

I will paste more information if needed, but does anyone have any idea as to how this could be? I am going to go out on a limb here and assume that I should be returning something and using a pass by value function instead, but I'm not entirely sure how to proceed from here. Thank you in advance for the help and kind words.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    You passed a copy of a pointer. If you point that pointer to something new via malloc you're modifying different memory. Either pass a pointer to a pointer or allocate the struct outside the function. Consider a [mcve], you may have more issues we can't see. – Retired Ninja Jan 25 '19 at 01:57
  • This is not a duplicate question. At least, it's not a duplicate of the one that is posted. – Darrin Cullop Jan 25 '19 at 02:16
  • @DarrinCullop: In this question, the function receives a pointer parameter `players`, allocates space, sets the pointer to point to the space, and then returns, losing the pointer. In the proposed original, the function receives a pointer parameter `s`, allocates space, sets the pointer to point to the space, and then returns, losing the pointer. What significant difference do you perceive? – Eric Postpischil Jan 25 '19 at 02:50
  • I guess it's not that different. – Darrin Cullop Jan 28 '19 at 22:06

1 Answers1

1

You're passing by value and you need to pass by reference. If you pass the pointer by reference it will work. Try this instead:

void scanning(KBLottoPlayer** players){
    int i, j;

    FILE * ifp = NULL;
    ifp=fopen("KnightsBall.in", "r");

    fscanf(ifp, "%d", &people);
    *players = malloc(people * sizeof(KBLottoPlayer));

    for(i=0;i<people;i++){
        fscanf(ifp, "%s%s", &(*players)[i].lastName, &(*players)[i].firstName);

        for(j=0;j<6;j++){
            fscanf(ifp, "%d", &(*players)[i].numbers[j]);
        }
    }
}

And then when you call scanning, pass in the address of the variable, like this:

 scanning(&players);

It basically passes a pointer to a pointer so that you can modify it in the other function.

More information:

Darrin Cullop
  • 1,170
  • 8
  • 14