c语言地址传递函数

我编了如下代码:
#include"stdio.h"
struct stu
{
int i;
int j;
};
int add(struct stu &g)
{
int k;
k=stu.i+stu.j;
return k;
}
main()
{
int k;
struct stu student;
student.i=1;
student.j=2;
k=add(student);
printf("%d",k);
}
编译时出现如下错误:
:\123\main.c(7) : error C2143: syntax error : missing ')' before '&'
e:\123\main.c(7) : error C2143: syntax error : missing '{' before '&'
e:\123\main.c(7) : error C2059: syntax error : '&'
e:\123\main.c(7) : error C2059: syntax error : ')'
e:\123\main.c(19) : warning C4013: 'add' undefined; assuming extern returning int
Error executing cl.exe.

main.obj - 4 error(s), 1 warning(s)
哪位高手帮忙看看如何修改谢了

int add(struct stu &g)
标准c是不支持这种传引用的语法的。。想做到类似效果只能函数里用 *g,调用时候用 &value 之类的

k=stu.i+stu.j; 这里stu应该是g
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-04-17
#include"stdio.h"
struct stu
{
int i;
int j;
};
int add(struct stu &g)
{
int k;
k=g.i+g.j;
return k;
}
main()
{
int k;
struct stu student;
student.i=1;
student.j=2;
k=add(student);
printf("%d",k);
}
第2个回答  2009-04-17
int add(struct stu g) //struct stu &g ?
{
int k;
k=g.i+g.j; //<--
return k;
}
相似回答