通过使用引用传递意外错误

时间:2019-01-29 00:21:05

标签: c

我想使用按引用传递,但编译器表示错误:预期为';'。

花了很多时间找到错误之后,我仍然不明白为什么代码不起作用。

然后,在我修复了丢失的分号之后,我又遇到了另一个错误,说是预期的')'

int around(int &count1,int &count2,int &count3);

int volume(int &v1,int &v2,int &v3);

int large(int &a1,int &a2,int &a3);



int main() {
  int l,w,h,sum;

  printf("Input Length : ");
  scanf("%d",&l );

  printf("Input Width : ");
  scanf("%d",&w );

  printf("Input Height : ");
  scanf("%d",&h );
  printf("\n\n");

  sum = around(l,w,h);

  printf("Around = %d\n",sum );

  sum = volume(l,w,h);
  printf("Volume = %d\n",sum );

  sum = large(l,w,h);
  printf("Large = %d\n",sum );
  return 0;
}


int around(int &count1,int &count2, int &count3){
  int value;
  value = 4*(count1+count2+count3);

  return (value);
}

int volume(int &v1,int &v2,int &v3){
  int val;
  val = v1*v2*v3;

  return (val);
}

int large(int &a1,int &a2,int &a3){
  int sm;
  sm = 2*((a1*a2) + (a1*a3) + (a2*a3));
  return (sm);
}

1 个答案:

答案 0 :(得分:3)

引用,即像&中的int around(int &count1,int &count2,int &count3)开头的参数是C ++功能,在C语言中不可用。

无论如何,您的函数实际上都没有改变参数,因此“按引用”传递毫无意义。

更改所有看起来像的函数(及其前向声明)

int around(int &count1,int &count2,int &count3) ...

进入

int around(int count1, int  count2, int  count3) ...

应该为C编译器解决问题。

对于C ++编译器,它还是可以工作的。