-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct2.c
56 lines (42 loc) · 992 Bytes
/
struct2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <stdlib.h>
struct item {
char *itemname;
int qty;
float price;
float amount;
};
void readitem(struct item * i);
void printitem(struct item *i);
int main()
{
struct item itm;
struct item *pItem;
pItem = &itm;
pItem -> itemname = (char *) malloc(30*sizeof(char));
if (pItem == NULL)
exit(-1);
//read item
readitem(pItem);
//print item
printitem(pItem);
free(pItem->itemname);
return 0;
}
void readitem(struct item * i)
{
printf("Enter product name: ");
scanf("%s", i -> itemname);
printf("\n Enter price: ");
scanf("%f", &i -> price);
printf("\n Enter Quantity: ");
scanf("%d", &i -> qty);
i -> amount = (float)i -> qty * i -> price;
}
void printitem(struct item * i)
{
printf("\n Name: %s", i-> itemname);
printf("\n Price: %.2f",i->price);
printf("\n Quantity: %d",i->qty);
printf("\n Total Amount: %.2f",i->amount);
}