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 9213169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:41:02+00:00 2026-06-18T01:41:02+00:00

I am fairly new to the C language and allocating memory/using pointers in general.

  • 0

I am fairly new to the C language and allocating memory/using pointers in general. Anyway, I was experimenting with reading a file, putting those values in a struct, etc. I know exactly what I want to do and of course the program runs but the output is incorrect and some kind of jumbled numbers and letters.

There is a text file with new information for each line. Each line represents one object.

This is how a line in the file might look:

meat sirloin 6.55 8 8.50 4

Overall I want to be able to store all of my PRODUCT objects in an array (so I have an array of structs). So I attempted to allocate memory with a pointer, use a line count, and then send the pointer to a function called read. In read I add each struct to the array via a pointer. The program doesn’t crash, the output is just not correct and I have no clue why not. It’s prob something with pointers. If anyone could help me I would really appreciate it. Any help at all would be great.

      //prototype
void read(pointerToArr);


typedef struct
{
    char supType[15];
    char prodName[15];
    double wholePrice;
    int quantWhole;
    double retPrice;
    int retProdQuantity;
}PRODUCT;

FILE *fr;
int lineCount = 0;
int main()
{
    PRODUCT *ptr;

    int i;
    char check[50];


     fr = fopen("ttt.txt", "r");

      while(fgets(check, sizeof(check), fr)!= NULL)
      {
          if(check[0] != '\n')
          {
              lineCount++;
          }
      }
    // allocate memory for array based on line count.
     PRODUCT prodContainter[lineCount];
     ptr = (PRODUCT*)malloc(sizeof(PRODUCT)*lineCount);
     ptr = prodContainter;

     read(ptr);

      //print after adding to array via pointer from other
      //function. this was a test.

      for(i = 0; i < lineCount; i++)
      {
          printf("%s ", prodContainter[i].supType);
          printf("%s ", prodContainter[i].prodName);
          printf("%f ", prodContainter[i].wholePrice);
          printf("%d ", prodContainter[i].quantWhole);
          printf("%f ", prodContainter[i].retPrice);
          printf("%d\n\n", prodContainter[i].retProdQuantity);

      }

    return 0;
}



void read(PRODUCT *pointerToArr)
{

    // objective in this method is to read in data from the file, create an object for every line and
    // then use the pointer array to add those objects to prodConstainer up above.

       char supplyName[15];
       char productName[15];
       double wholeP = 0;
       int  quantityWhole = 0;
       double retailPrice = 0;
       int retailProductQuant = 0;

    while(fscanf(fr, "%s %s %lf %d %lf %d", supplyName, productName, &wholeP, &quantityWhole, &retailPrice, &retailProductQuant) == 6)
    {
        PRODUCT record;
        int i;

        strcpy(record.supType, supplyName);
        strcpy(record.prodName, productName);
        record.wholePrice = wholeP;
        record.quantWhole = quantityWhole;
        record.retPrice = retailPrice;
        record.retProdQuantity = retailProductQuant;

        for(i = 0; i < lineCount; i++)
        {
            pointerToArr[i] = record;
        }
    }

    fclose(fr);
}
  • 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-18T01:41:03+00:00Added an answer on June 18, 2026 at 1:41 am

    You never rewind the file, so all the reading after you count the number of lines fails.

    What you’re printing is just what happens to be in memory.

    There are many ways to fix this, of course.

    1. Rewind the file, using rewind()
    2. Close the file and let your read() function (whose name collides with a POSIX standard function, btw) re-open the file. This would also involve removing the scary global variable fr.
    3. Re-structure so you never count the number of lines, by just reading and letting the ptr array grow as necessary (see realloc()).

    Also, you should really avoid casting the return value of malloc() in C. This:

    ptr = (PRODUCT*)malloc(sizeof(PRODUCT)*lineCount);
    

    is better written as:

    ptr = malloc(lineCount * sizeof *ptr);
    

    This does away with the cast, and also uses sizeof on a value of the type pointed at to automatically compute the proper number of bytes to allocate.

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

Sidebar

Related Questions

I am using C#, I'm fairly new to the language but I have used
I am fairly new to XCode and the Objective-C language. When I am instantiating
I am fairly new to XCode and the Objective-C language. When I am instantiating
I am very new to assembly language, and fairly new to C. I have
I have a question about Scheme. I am fairly new to the language, and
I'm fairly new to C programming language and packet capturing. Right now I'm writing
I am fairly new to Servlet Filters and have basically joined a project using
So I'm fairly new to Assembly language I have a pretty solid grasp on
I'm fairly new to the C programming language but I know that it is
I am fairly new to C# language so I just do not understand why

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.