I'm having trouble with my first program using malloc. My problem is that the program crashes when it executes the free() line. I have no idea why this is happening and would like to know how to prevent it from happening.
#include <stdio.h>
#include <stdlib.h>
struct product{
int cost;
char thing[20];
};
int main()
{
int amount;
scanf("%d", &amount);
getchar();
struct product *products;
products = (struct product *) malloc(amount);
for (int i = 0; i < amount; i++)
{
printf("Thing of %d ", (i + 1));
gets(products[i].thing);
printf("Cost of %d: ", (i + 1));
scanf("%d", &products[i].cost);
getchar();
}
free(products);
return 0;
}