问题
I found this program online to practice C. When I tried to compile this program in Code blocks, I am getting this error "error: expected ';', ',' or ')' before '&' token| " in two places (mentioned in the codes). It would be really helpful, if someone could explain me the reason for the error.
#include<stdio.h>
int f1(int x,int y)
{
x=x+2;
y=y+3;
return x+y;}
int f2(int &x,int y) //error: expected ';', ',' or ')' before '&' token|
{
x=x+2;
y=y+3;
return x+y;
}
int f3(int *x,int *y)
{
*x = *x+2;
*y = *y+3;
return *x+*y;
}
int f4(int x,int &y,int *z)//error: expected ';', ',' or ')' before '&' token|
{ x=x+y;
y=*z+3;
z=&x;
*z=y*2;
return *z;
}
main()
{
int k=3,m=5,r=0;
printf("1) %d %d %d\n",k,m,r);
r=f1(k,m);
printf("2) %d %d %d\n",k,m,r);
r=f2(k,m);
printf("1) %d %d %d\n",k,m,r);
r=f3(&k,&m);
printf("1) %d %d %d\n",k,m,r);
r=f4(k,m,&r);
printf("1) %d %d %d\n",k,m,r);
return 0;
}
回答1:
This is not a C program. This is a C++ program. It does not use too many C++ features, but it uses enough to make this code non-compilable as C code.
Either compile it as C++ or convert it to C. The latter would require rewriting quite a few lines of that code.
回答2:
Line 11 Should be int f2(int *x,int y)
The code you posted is C++ code. Not C code. You can not execute C++ code on C compiler.
回答3:
In C you should not use reference In function definition AS like C++.
You do not need to use pointer even, because you are just passing values.
int f2(int &x,int y)
^^
Modify above function definition
int f2(int x,int y)
This line also
int f4(int x,int &y,int *z)
Modify above function definition
int f4(int x,int y,int *z)
and declare your functions.
来源:https://stackoverflow.com/questions/19782983/error-expected-or-before-token-on-a-simple-c-program-found-on