zhaoduola 2015-05-15 01:55 采纳率: 0%
浏览 1585

结构体作为参数该怎么船传值

struct student{
int number;
int score;
};
int main()
{
struct student st;
sc(&st);
printf("%d,%d\n",st.number,st.score);
ptf(st);
return 0;
}
int sc(struct student **st)
{
*st=(struct student *)malloc(sizeof(st));
scanf("%d",&(*st)->number);
scanf("%d",&(*st)->score);
return 1;
}
int ptf(struct student *st)
{
printf("%d,%d\n",st->number,st->score);
return 1;
}
ptf函数的printf能打印正确的值,主函数的printf打印出来的值是“136261640,4788944“,这是哪的问题,还有结构体是怎么传的,什么时候加&什么时候不用加&

  • 写回答

2条回答 默认 最新

  • oyljerry 2015-05-15 02:19
    关注
     int sc(struct student& st)
    

    可以直接传引用,这样函数里面直接用。

    评论

报告相同问题?