John
John

Reputation: 89

Structure in C - Access value

I am trying to play around with structure and this is what I have:

  #DEFINE LINEAR 1
  int val;
  struct Item
  {
      double price;
      int weight;
      char Tax;
      int quant;
      int minQuant;
      char namel[30];  
  };

  double totalAfterTax(struct Item item);

 int main() {
   struct Item I[21] = 
   {
     { 4.4,275,8,10,2,"Royal Apples" },
     { 5.99,386,18,20,4,"Melon"},
   };
   val = display(I[0], LINEAR);
   return 0;
 } //main end


   void display(struct Item item, int linear){

   struct Item i1; 
   printf ("%d ", i1.quant); 
   return;
   }

Now, problem is i1.quant is not printing 8 as it is supposed to. I am not sure why?

Please advise?

Upvotes: 0

Views: 88

Answers (2)

b107
b107

Reputation: 88

I think the line below should be removed

struct Item i1;

and you should replace printing line with the following:

printf ("%d \n",item.quant);

According to your program it should give you an error or warning and after making above changes, output should be "10" not 8. As you are printing item.quant.

Upvotes: 0

dvnguyen
dvnguyen

Reputation: 3022

Inside the display function, you define an 'empty' (uninitialized) struct Item. I believe what you wanted to print should be item.quant:

void display(struct Item item, int linear){
    printf ("%d ", item.quant); 
}

Upvotes: 3

Related Questions