0

I just get this when I try to run it error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token

it's complaining about this line

int read_values(double &sum) {

so i want to pass sum, and then edit it directly. How do you do that in C? Thanks guys.

#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;
}
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
user2054534
  • 181
  • 3
  • 5
  • 17
  • Wellcome to SO. Please check this site and your search engine for similar questions before asking. This one is a possible duplicate of [Passing pointer argument by reference under C?](http://stackoverflow.com/questions/1825794/passing-pointer-argument-by-reference-under-c) – Jens Gustedt Feb 11 '13 at 23:17

2 Answers2

3

C doesn't do references, only raw pointers.

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


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

}
James
  • 9,064
  • 3
  • 31
  • 49
1

I think you're trying to do this:

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;
}
mikhail
  • 5,019
  • 2
  • 34
  • 47