C中的结构 - 访问值

时间:2016-11-17 02:52:56

标签: c

我正在尝试使用结构,这就是我所拥有的:

  #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;
   }

现在,问题是i1.quant没有按照预期打印8。我不确定为什么?

请指教?

2 个答案:

答案 0 :(得分:3)

display功能中,您可以定义一个空的' (未初始化)struct Item。我相信你想要打印的内容应该是item.quant

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

答案 1 :(得分:0)

我认为应删除以下一行

struct Item i1;

您应该使用以下内容替换打印行:

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

根据您的程序,它应该给您一个错误或警告,并在进行上述更改后,输出应为" 10"不是8.在打印时item.quant