0

当我尝试运行它时,我才得到这个 error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token

它在抱怨这条线

int read_values(double &sum) {

所以我想传递总和,然后直接编辑它。你如何在 C 中做到这一点?多谢你们。

#include <stdio.h>

/*
    Read a set of values from the user.
    Store the sum in the sum variable and return the number of values read.
*/
int read_values(double &sum) {
  int values=0,input=0; double sum2=0;
  sum2 = sum;
  printf("Enter input values (enter 0 to finish):\n");
  scanf("%d",&input);
  printf("%d\n", input);
  while(input != 0) {
    values++;
    sum2 += input;

    scanf("%d",&input);
  }

  return values;
}

int main() {
  double sum=0;
  int values;
  values = read_values(sum);
  printf("Average: %g\n",sum/values);
  return 0;
}
4

2 回答 2

3

C 不做引用,只做原始指针。

main() {
    ...
    values = read_values(&sum);
    ...
}


int read_values(double* sum)
{
    ...
    sum2 = *sum;
    ...

}
于 2013-02-11T22:59:40.830 回答
1

我认为您正在尝试这样做:

int read_values(double *sum) {
  int values=0,input=0;
  printf("Enter input values (enter 0 to finish):\n");
  scanf("%d",&input);
  printf("%d\n", input);
  while(input != 0) {
    values++;
    *sum += input;
    scanf("%d",&input);
  }
  return values;
}

int main() {
  double sum=0;
  int values;
  values = read_values(&sum);
  printf("Average: %g\n",sum/values);
  return 0;
}
于 2013-02-11T23:04:19.513 回答