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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:41:10+00:00 2026-06-16T08:41:10+00:00

Possible Duplicate: Using Dynamic Memory allocation for arrays I originally had this program store

  • 0

Possible Duplicate:
Using Dynamic Memory allocation for arrays

I originally had this program store prices with a quantity size of ten and realized that I wanted to make the program more dynamic because I may need to store more than ten items at some given point. I’m having difficulties understanding on how to reallocate additional memory so that I can store any amount of items I would need. Is this the correct way of handling this task?

main function:

double *purchases = (double*)malloc(QUANTITY_SIZE);

outside function

double startShopping(double *purchases, double *taxAmount, double *subTotal, double *totalPrice)
{
    double itemPrice = 0.00;
    double* storeMoreItems;

    for(int i = 0; i < QUANTITY_SIZE; *subTotal +=purchases[i++])
    {
        while(itemPrice != -1)
        {
            printf("Enter the price of the item :");
            scanf("%lf", &itemPrice); 

            storeMoreItems = (double*)realloc(storeMoreItems, i * sizeof(int));

            if(storeMoreItems != NULL)
            {
                storeMoreItems = purchases;
                purchases[i-1] = itemPrice;
            }

           else
           {
               free(purchases);
           }
       }
  }

  displayCart(purchases);

  *taxAmount = *subTotal * TAX_AMOUNT;

  *totalPrice = *taxAmount + *subTotal;

  printf("\nTotal comes to : $%.2lf\n", *totalPrice);

  return *totalPrice;
}
  • 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-16T08:41:11+00:00Added an answer on June 16, 2026 at 8:41 am

    double* purchases = (double*)malloc(sizeof(double)*QUANTITY_SIZE);

    What is more:

    storeMoreItems = (double*)realloc(storeMoreItems, i * sizeof(double));

    You tried to allocate the i*sizeof(int) and then casted it to double*. When double and int have different sizes your code would have a difficult to find bug.

    The next thing:

    When i is equal to 0 you allocate memory with initial size 0 bytes (i*sizeof(int)) and then try to use it. It won’t work. Try to change your loop in this way: for (int i = 1, i <= QUANTITY_SIZE;... and keep purchases[i-1].

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

Sidebar

Related Questions

Possible Duplicate: C/C++: Array size at run time w/o dynamic allocation is allowed? In
Possible Duplicate: Using var outside of a method I've searched for this a bit,
Possible Duplicate: Using ApplicationSettings to store Checked property for WinForms RadioButtons I have three
Possible Duplicate: Matrix Arithmetic using Vectors in C++ causing segmentation faults I created this
Possible Duplicate: using dynamic IN clause in MSSQL I have one SQL statement which
Possible Duplicate: How to access object using dynamic key? I have multiple select with
Possible Duplicate: filter using Q object with dynamic from user? I am working on
Possible Duplicate: Need help in dynamic query with IN Clause I am using SQL
Possible Duplicate: Auto-size dynamic text to fill fixed size container How can I get
Possible Duplicate: Dynamic object property name I have an object like this var localAppConfig

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.