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

  • Home
  • SEARCH
  • 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 8323867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:51:25+00:00 2026-06-08T23:51:25+00:00

I have a struct defined as typedef struct { char* name; char* ID; int

  • 0

I have a struct defined as

typedef struct
{
    char* name;
    char* ID;
    int marks;

    char* details;

} Student;

And another as

typedef struct
{
    int n;
    Student* stud_array;
} Batch;

A file has entries of various batches of student in a particular format, and I’m reading from that file to populate BatchArray of type Batch*

After each field is populated I am using

puts(BatchArray[no_of_batches-1].stud_array[i].name);
puts(BatchArray[no_of_batches-1].stud_array[i].ID);
printf("%d\n",BatchArray[no_of_batches-1].stud_array[i].marks);

to see the result. And it gives me the right output.

However, after everything is done, when I want to iterate through BatchArray, only the marks field in each struct retains the value. The name and ID are shown to be some random garbage value.

The iteration code is standard. Nevertheless, here it is.

   for(i = 0; i < no_of_batches; i++)   {
        currBSize = BatchArray[i].n;
        printf("Batch %d\n", (i+1));
        printf("Batch size %d\n", currBSize);
        for(j = 0; j < currBSize; j++)  {
            puts(BatchArray[i].stud_array[j].name);
        }
    }

The problem that I’m working on needs to find the average marks of each batch, so that’s not a problem. But I’d like to know why the other fields get reset to garbage values.

Can someone help with this?

Edit : here is how I’m populating the fields.

The file consists of entries like this

3
Name1 ID1 marks1
Name2 ID2 marks2
Name3 ID3 marks3
2
Name4 ID4 marks4
Name5 ID5 marks5

and this is the code.

no_of_batches = 0;
            infileptr = fopen (infilename, "r");

            BatchArray = (Batch *) malloc(sizeof(no_of_batches));

            int MAX_BUFF = 100;
            char currLine[MAX_BUFF];    

            while (fgets(currLine, MAX_BUFF, infileptr) != NULL)    {

                no_of_batches++;
                BatchArray = (Batch *) realloc(BatchArray, no_of_batches*sizeof(Batch));
                currBatchSize = atoi(currLine);
                BatchArray[no_of_batches-1].n = currBatchSize;
                printf("currBatchSize : %d\n",BatchArray[no_of_batches-1].n);
                BatchArray[no_of_batches-1].stud_array = (Student *) malloc(currBatchSize*sizeof(Student));
                for(i = 0; i < currBatchSize; i++)  {
                    fgets(currLine, MAX_BUFF, infileptr);
                    currLine[strlen(currLine)-1] = '\0';

                    BatchArray[no_of_batches-1].stud_array[i].details = currLine;

                    //getting the Name from currLine
                    j = 0;
                    len = strlen(currLine);
                    char buildName[len];
                    while(currLine[j] != ' ')   {
                        buildName[j] = currLine[j];
                        j++;
                    }

                    buildName[j] = '\0';
                    BatchArray[no_of_batches-1].stud_array[i].name = buildName;

                    j++;
                    //getting the ID from currLine
                    k = 0;
                    char buildID[len];
                    while(currLine[j] != ' ')   {
                        buildID[k] = currLine[j];
                        j++;
                        k++;
                    }

                    buildID[k] = '\0';
                    BatchArray[no_of_batches-1].stud_array[i].ID = buildID;

                    puts(BatchArray[no_of_batches-1].stud_array[i].name);
                    puts(BatchArray[no_of_batches-1].stud_array[i].ID);

                    //getting the marks from currLine
                    k = 0;
                    j++;
                    char buildMarks[len];
                    while(currLine[j] != '\0')  {
                        buildMarks[k] = currLine[j];
                        j++;
                        k++;
                    }

                    buildMarks[k] = '\0';
                    BatchArray[no_of_batches-1].stud_array[i].marks = atoi(buildMarks);
                    printf("%d\n",BatchArray[no_of_batches-1].stud_array[i].marks);
                }
                puts("");
            }
  • 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-08T23:51:27+00:00Added an answer on June 8, 2026 at 11:51 pm

    Instead of

        BatchArray[no_of_batches-1].stud_array[i].name = buildName;
    

    write

        BatchArray[no_of_batches-1].stud_array[i].name = strdup(buildName);
    

    The problem with the first case is that name points to buildName, which is an array on the stack and will be overwritten whenever the contents of the stack change.

    In the second case you’ve copied the array to newly allocated heap memory (as with malloc or realloc), which will remain unmodified until you free it.

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

Sidebar

Related Questions

I have a struct defined as: struct { char name[32]; int size; int start;
I have a structured defined as typedef struct{ char string1 char string2 int number1
I have defined a struct ABC to contain an int ID, string NAME, string
I have a struct like so, typedef struct Player { char *name; char *heroID;
I have this struct: #define sbuffer 128 #define xbuffer 1024 typedef struct{ char name[sbuffer];
So I've got this. #define MAX_MENU_OPTIONS 1 typedef struct _NEW_MENU_OPTION { char* name; int
I have defined the following struct in C: typedef struct point{ float x; float
I have defined a custom struct which I need to send over to another
I have an adjacency_list graph defined as follows: struct VertexProperties{ std::string name; ... };
I have a type defined like this: struct DynTab_s { int object_count; //other fields

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.