【发布时间】:2021-03-25 12:07:03
【问题描述】:
基本上我试图通过函数为结构分配一个值,这样我分配的值在 main() 之后是相同的。
结构
struct member{
char name;
int age;
}m1;
void assigntostruct(struct member str,int age){
str.age = age;
main(){
int age=10;
assigntostruct(m1,age);
printf("%d",age);
}
我试过这样,值“age”被传递给str.age,但是当我printf它返回0。
【问题讨论】:
-
您至少缺少
assigntostruct的右大括号。你能发布至少可以编译的代码吗? -
不需要将
m1传递给函数,因为它是在全局范围内声明的,它已经可以从assigntostruct()访问,只需使用m1.age = age;就可以了。跨度>
标签: c struct pass-by-reference function-declaration