尝试将x,y,z作为参数传递

时间:2013-04-07 23:17:28

标签: c++

错误表示在我调用foo的main行中的int之前的预期主表达式。我不明白,如何解决它。我究竟做错了什么?

#include <stdio.h>
#include <stdlib.h>

int foo(int *a, int *b, int c) {
    /* Set a to double its original value */
   *a = *a * *a;
    /* Set b to half its original value */
   *b = *b / 2;
    /* Assign a+b to c */
   c = *a + *b;
    /* Return c */
   return c;
}

int main() {
    /* Declare three integers x, y, z and initialize them to 5, 6, 7 respectively */
   int x = 5, y = 6, z = 7;
    /* Print the values of x, y, z */
   printf("X value: %d\t\n", x);
   printf("Y value: %d\t\n", y);
   printf("Z value: %d\t\n", z);
    /* Call foo() appropriately, passing x, y, z as parameters */
   foo(int *x, int *y, int z);
    /* Print the value returned by foo */
   printf("Value returned by foo: %d\t\n", foo(x, y, z));
    /* Print the values of x, y, z again */
   printf("X value: %d\t\n", x);
   printf("Y value: %d\t\n", y);
   printf("Z value: %d\t\n", z);
    /* Is the return value different than the value of z?  Why? */

  return 0;
}

2 个答案:

答案 0 :(得分:3)

您正在调用它,您需要将变量的地址作为参数传递:

foo(&x,&y,z);

另外,由于你是通过值而不是引用来传递z,你应该把它分配给函数的返回值(看起来你正在尝试做什么?):

z=foo(&x,&y,z);

答案 1 :(得分:1)

你正在声明要传递它们的变量..

foo(&x, &y, z);