The price and
quantity of items stocked in a store changes every day. They may either increase or decrease. The program in Fig.11.15 reads the
incremental values of price and quantity and computes the total value of the
items in stock.
The program
illustrates the use of structure pointers as function parameters. &item,
the address of the structure item,
is passed to the functions update() and
mul(). The formal arguments product and stock, which
receive the value of &item, are
declared as pointers of type struct
stores.
STRUCTURES AS FUNCTION PARAMETERS
Using structure pointers
Program
struct stores
{
char
name[20];
float price;
int
quantity;
};
main()
{
void update(struct stores *, float,
int);
float
p_increment, value;
int q_increment;
struct stores item = {"XYZ",
25.75, 12};
struct stores *ptr = &item;
printf("\nInput increment
values:");
printf(" price increment and
quantity increment\n");
scanf("%f %d",
&p_increment, &q_increment);
/* - - - - - - - - - - - - - - - - - - - - -
- - - - - - */
update(&item, p_increment,
q_increment);
/* - - - - - - - - - - - - - - - - - - - - -
- - - - - - */
printf("Updated values of
item\n\n");
printf("Name : %s\n",ptr->name);
printf("Price : %f\n",ptr->price);
printf("Quantity : %d\n",ptr->quantity);
/* - - - - - - - - - - - - - - - - - - - - -
- - - - - - */
value
= mul(&item);
/* - - - - - - - - - - - - - - - - - - - - -
- - - - - - */
printf("\nValue of the item =
%f\n", value);
}
void update(struct stores *product, float p,
int q)
{
product->price += p;
product->quantity += q;
}
float mul(struct stores
*stock)
{
return(stock->price *
stock->quantity);
}
Output
Input increment values: price increment and
quantity increment
10 12
Updated values of item
Name : XYZ
Price
: 35.750000
Quantity
: 24
No comments:
Post a Comment