Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:45:48+00:00 2026-06-15T16:45:48+00:00

I have written this function (below). It should read in a file line by

  • 0

I have written this function (below). It should read in a file line by line. Edit the line and put certain words/characters in various functions. The functions are then put into an array (malloc) of “entrant” structs.

The problem is that when I then exit the loop and try to print out the array, the last struct name variable put into the array is the only name variable printed out. If I try to print out the first 10 struct name variables of the array for instance, the last struct name variable will be printed out 10 times. The integer (number variable) works fine however.

I use fgets() to read lines of the files and I break from the loop at the end of the file. Possibly fgets() could be the problem but that seems unlikely…

I do not know what the problem is but as I said the number variable prints fine but the name prints out the last one only.

But taking the information from the file does not. If put my loop print statement inside the while loop it still doesn’t work.

One thing to note is that putting the line into the separate variables using strtok, strcpy and strcat is working fine. I don’t think it’s that that is causing it.

struct entrant {
  int number;
  char *name;
};


void read_in_entrants(char fileName[]) {
    FILE *fr;
    fr = fopen(fileName, "rt");

    //Init.
    struct entrant * entrant_array = (struct entrant*) malloc(sizeof (struct entrant));
    int j, temp, k = 0;
    int *number;
    char line[80];
    char name[80];
    char surname[80];

    while(1) {
        //Read line in file.
        fgets(line, 80, fr);

        //If at end of file break from loop.
         if (feof(fr)) break;

        //Edit line to be stored in variables.
        number = strtok(line, " ");
        strcpy(name, strtok(NULL, " "));
        strcpy(surname, strtok(NULL, " "));
        strcat(name, " ");
        strcat(name, surname);

        //Add variables to struct array.
        entrant_array[k].number = atoi(number);
        entrant_array[k].name = name;
        printf(" %d %s\n", entrant_array[k].number, entrant_array[k].name);

        //Handle malloc array memory.
        temp = realloc(entrant_array, (k + 2) * sizeof (struct entrant));
        if (temp != NULL) {
            entrant_array = temp;
        } else {
            free(entrant_array);
            printf("Error in memory allocation!");
            return 1;
        }
        //Increase k.
        k++;
    }

    //Print struct values in array.
    for (j = 0; j < 10; j++) {
        printf(" %d %s", entrant_array[j].number, entrant_array[j].name);
    }

   //Shut down.
   free(entrant_array);
   fclose(fr);
}

Anyway, I’ve been struggling with this literally for hours now and I just can’t fix it. My knowledge of C is extremely limited and I’m pretty lost as to what I need to do to fix this.

Thanks for the help, it’s very much appreciated 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:45:48+00:00Added an answer on June 15, 2026 at 4:45 pm

    entrant.name is a pointer, which you are assigning to the same local char array. So all pointers point to the same variable, which will just contain the last thing you read. Until it goes out of scope, at which point it will point at random memory.

    The quickest fix would be to replace

    entrant_array[k].name = name;
    

    with

    entrant_array[k].name = strdup(name);
    

    But you will need to remember to free that memory later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a haskell file test.hs. In this file I have written a function
I have written a function that attempts to read a child process's command line
I have written this function to auto correct gender to M or F from
I have written a function to print database table to an array like this
I have written my own function, which in C would be declared like this,
I have written a function for deleting object preiodically. function add(variable, expireSecond){ this.list[variable] =
I have written this code inside a servlet to delete certain records from three
I have written a function below that takes a pointer to the front of
I have written this code. Its not working but with this you should get
i have created below function in a file info.php to debug variable & data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.