2013-04-07 22 views
0

錯誤表示預期的主要表達式在int之前,在我稱爲foo的main行中。我不明白,以及如何解決它。我究竟做錯了什麼?試圖通過x,y,z作爲參數

#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; 
} 
+0

將其重新標記爲'C++',因爲這不是'C#'問題。 – Corey 2013-04-07 23:19:08

+0

除了下面的答案,z在你的代碼中永遠不會被foo讀取,它有點令人困惑。 – chrisw 2013-04-07 23:23:56

回答

3

你是不正確調用它,你需要把變量的地址傳遞的參數:另外

foo(&x,&y,z); 

,因爲你是按值傳遞ž不引用,你應該給它分配該函數的返回值(它看起來像那是你正在嘗試做的?):

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

你宣佈你的意思,其中的變量路過他們..

foo(&x, &y, z); 
相關問題