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;
}