0

错误在我调用 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;
}
4

2 回答 2

3

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

foo(&x,&y,z);

此外,由于您通过值而不是引用传递 z,因此您可能应该将其分配给函数的返回值(看起来这就是您想要做的事情?):

z=foo(&x,&y,z);
于 2013-04-07T23:19:29.170 回答
1

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

foo(&x, &y, z);
于 2013-04-07T23:19:30.943 回答