void print(part *item, int part_count) {
int i=0;
for (i=0; i<part_count; i++) {
printf("Item number: %d\n", i + 1);
printf("Item name: %s\n", item[i].name);
printf("Item price: $%f\n", item[i].price);
printf("Item quantity: %d\n", item[i].quantity);
}
}
i want to print an array of structs created with a different function. I have looked but have yet to find a different way to print them or what i am doing wrong in the print statements. My program compiles but crashes upon running.
ok well it is good to know the problem is not within these statements. That was frustrating me. here is the add function.
void add(part *item, int *part_count)
{
if (!item)
{
item = malloc(sizeof(part));
}
item = realloc(item, sizeof(part) * *part_count + 1);
item[*part_count].name = malloc(sizeof(char)*64); // max of 64 characters
printf("Please enter item name: ");
scanf("%65s", item[*part_count].name);
printf("Please enter item price: ");
scanf("%f", &item[*part_count].price);
printf("Please enter item quantity: ");
scanf("%d", &item[*part_count].quantity);
*part_count = *part_count+ 1;
}
item[i].nameis not null-terminated...print, and sets up the array that you print.part_countparameter. Perhaps asizeofinstead of the number of structures in the array?