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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:20:02+00:00 2026-06-17T14:20:02+00:00

I’ve got two different arrays that I’m using. With one, I’m getting the exact

  • 0

I’ve got two different arrays that I’m using. With one, I’m getting the exact results that I want, the other, not so much. I’m filing the arrays with by reading from a text file similar to this:

2597
283



4
723
21
82
426

The first five lines would be the customer IDs. There is always 5 lines but they don’t always have a value. The next line is the number of vendors, then followed by the vendor ids.

void use_arrays()
{
    int
        i,
        customer_count,
        *customer_ids,
        vendor_count,
        *vendor_ids;

    customer_ids = malloc(sizeof(int));
    vendor_ids = malloc(sizeof(int));
    fill_arrays(&customer_count, customer_ids, &vendor_count, vendor_ids);

    for (i = 0; i < customer_count; i++)
    {
        printf("Customer[%d]: %d\n", i, customer_ids[i]);
    }

    for (i = 0; i < vendor_count; i++)
    {
        printf("Vendor[%d]: %d\n", i, vendor_ids[i]);
    }

    free(customer_ids);
    free(vendor_ids);
}

void fill_arrays(int *customer_count, int *customer_ids, int *vendor_count, int *vendor_ids)
{
    int
        i,
        *temp,
        customer_id,
        vendor_id,
        num_cust = 0;
    FILE
        *inp_file;
    char
        *endptr = NULL,
        buffer[500];

    inp_file = fopen(g_filename, "r");

    for (i = 0; i < 5; i++) /* Can't be more than 5 customers */
    {
        fgets(buffer, sizeof(buffer), inp_file);
        customer_id = strtol(buffer, &endptr, 0);
        if (customer_id != 0)
        {
            customer_ids[i] = customer_id;
        temp = realloc(customer_ids, (i+2)*sizeof(int));
        if (temp != NULL) 
            {
               customer_ids = temp;
            } 
        else 
        {
               printf("Couldn't allocate memory\n");
            }
        num_cust++;
        }
    }
    *customer_count = num_cust;

    /* Next is number of vendor ids*/
    fgets(buffer, sizeof(buffer), inp_file);
    *vendor_count = strtol(buffer, &endptr, 0);

    temp = realloc(vendor_ids, *vendor_count*sizeof(int));
    if (temp != NULL) 
    {
        vendor_ids = temp;
    } 
    else 
    {
        printf("Couldn't allocate memory\n");
    }

    for (i = 0; i < *vendor_count; i++)
    {
        fgets(buffer, sizeof(buffer), inp_file);
        vendor_id = strtol(buffer, &endptr, 0);
        if (vendor_id != 0)
        {
        vendor_ids[i] = vendor_id;
        }
    }
    fclose(inp_file);
}

Once the arrays print out, customer_ids is showing the correct numbers but vendor_ids is printing out random numbers from memory. To be more frustrating, it prints the vendors correctly from inside fill_arrays.

  • 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-17T14:20:03+00:00Added an answer on June 17, 2026 at 2:20 pm

    If you want to modify vendor_ids the way you do in fill_arrays, then you have to pass it in as a pointer to a pointer:

    fill_arrays(int *customer_count, int *customer_ids, int *vendor_count, int **vendor_ids)
    

    Call it like this:

    fill_arrays(&customer_count, customer_ids, &vendor_count, &vendor_ids);
    

    Then you can realloc like so:

    temp = realloc(*vendor_ids, *vendor_count*sizeof(int));
    if (temp != NULL) 
    {
        *vendor_ids = temp;
    } 
    

    Also, at the end of your function:

    vendor_ids[i] = vendor_id;
    

    will have to change to

    (*vendor_ids)[i] = vendor_id;
    

    You will also have to make the same changes to customer_ids. The fact that customer_ids was working while vendor_ids wasn’t was probably due to your use of realloc. If realloc decides that the memory block has to be reallocated in a new location, you’ll run into these problems but if reallocates the memory in the same location, your pointer that you passed in is still pointing there. Since you never know if realloc is going to make that descision or not, both customer_ids and vendor_ids should be passed in as pointers to pointers.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
I want to count how many characters a certain string has in PHP, but

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.