2013-01-09 31 views
2

I am getting an error in the following program与PARAM所有功能(INT和,INT和)

#include<stdio.h> 
     void func(int &x,int &y){ 
     } 
     int main(){ 
      int a=10,b=6; 

      func(a,b); 
      return 0; 
     } 

Error:

prog.c:2: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token prog.c: In function ‘main’: prog.c:7: warning: implicit declaration of function ‘func’

but when I am changing function parameter type from (&) to (*) or any other type then it is working properly.

Like this:

#include<stdio.h> 
    void func(int *x,int *y){ 

    } 
    int main(){ 
     int a=10,b=6; 
     func(&a,&b); 
     return 0; 
    } 

Thanks in advance.

Nks

+5

这是正确的,C没有引用。作为克里斯说,你不能在c中使用引用(即&),因此可以使用 – chris

+0

。 c只支持指针。没有参考。 – Arpit

回答

3

你所得到的编译器错误,因为你没有写有效的C代码的隐式声明。 (int &x,int &y)没有任何意义,它看起来像你正试图在C.使用C++引用

2

没有通在C参考,使用的是C++语法在你的代码,为C你的代码应该是作为你在第二座提到。

2

通过引用传递是不是在C不允许。第二块代码是正确的事情..

你逝去的变量地址在实际参数,你需要收集他们的指针变量在形式参数在C语言..

func(&x, &y) // actual parameters 

void func(int *x, int *y) //formal parameters 
相关问题