I know it refers back to variable but I have never seen it used before, could someone please explain :o

void change_item( diner * d, float *total )
{
int item_num, quantity, new_item_num, new_quantity;
float price;
display_items(d);
do
{
printf("\nEnter item number to be added : ");
scanf(" %d", &item_num );
if( item_num < 1 || item_num > d->number_of_items )
printf("\nInvalid Choice !!!" );
else
{
quantity = d->item_list[item_num-1].quantity;
price = d->item_list[item_num-1].unit_price;


/* delete this item*/
d->total_sales -= quantity * price;
*total -= quantity * price;


/* get the new item details*/
display_item_list();
do
{
printf("\nEnter new item number : ");
scanf(" %d", &new_item_num );
if( new_item_num < 1 || new_item_num > MAX_ITEMS )
printf("\nInvalid Choice !!! ");
else
{
strcpy(d->item_list[d->number_of_items -1].description, description[ new_item_num-1 ] );
d->item_list[d->number_of_items -1].unit_price = unit_price[ new_item_num -1 ] ;


do                          /* get the quantity */
{
printf("\nEnter Quantity : ");
scanf(" %d", &new_quantity );
if( new_quantity < 1 )
printf("\nQuantity cant be less than one");
else
d->item_list[d->number_of_items -1].quantity = new_quantity;
}while( new_quantity < 1);


d->total_sales += new_quantity * unit_price[new_item_num-1];
*total += new_quantity * unit_price[new_item_num-1];
}


}while( new_item_num < 1 || new_item_num > MAX_ITEMS );
}


}while( item_num < 1 || item_num > MAX_ITEMS );
Dave Sinkula commented: Use code tags. +0

Recommended Answers

All 6 Replies

x->y is shorthand for x.(*y) which may make more sense if you're not used to the syntax.

x->y is shorthand for x.(*y)

Perhaps you mean (*x).y?

Perhaps you mean (*x).y?

so in my case its just referencing diner *d instead of writing it how? like
diner(*d)

#include <stdio.h>

struct type
{
   int member;
};

int main ( void )
{
   struct type object = {42}, *pointer_to_object = &object;
   printf("object.member               = %d\n", object.member);
   printf("pointer_to_object->member   = %d\n", pointer_to_object->member);
   printf("(*pointer_to_object).member = %d\n", (*pointer_to_object).member);
   printf("(&object)->member           = %d\n", (&object)->member);
   return 0;
}

/* my output
object.member               = 42
pointer_to_object->member   = 42
(*pointer_to_object).member = 42
(&object)->member           = 42
*/

The -> operator accesses a member of a struct/union/class like the . operator, but from a pointer variable.

beautiful thanks for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.