#include <stdlib.h>
struct product{
float price;
char productName[30];
};
int main(void){
struct product *pProducts;
int i, j;
int numberOfProducts;
printf("Enter the Number of products to store: ");
scanf("%d", &numberOfProducts);
pProducts = (struct product *) malloc(numberOfProducts * sizeof(struct product));
for(i=0; i < numberOfProducts; ++i){
printf("Enter a Product Name: ");
scanf("%s", &(pProducts+i)->productName);
printf("Enter a Product Price: ");
scanf("%e", &(pProducts+i)->price);
}
printf("Products Stored\n");
for(j=0; j < numberOfProducts; ++j){
printf("%s\t%.2f\n", (pProducts+j)->productName, (pProducts+j)->price);
}
free(pProducts);
return 0;
}
/*
* ./02_malloc_struct_store
Enter the Number of products to store: 3
Enter a Product Name: Egg
Enter a Product Price: .25
Enter a Product Name: Fish
Enter a Product Price: 11
Enter a Product Name: Bread
Enter a Product Price: 3
Products Stored
Egg 0.25
Fish 11.00
Bread 3.00 */
댓글 없음:
댓글 쓰기