#include<stdio.h>
struct Student
{ int age;char sex;
};int main(void){struct Student st;InputStudent(&st);printf("%d,%c",st.age,st.sex);}void InputStudent(struct Student *pstu)
{(*pstu).age=88;pstu->sex='F';}
老是出现警告,排查好多遍但是还是警告,最后想起小甲鱼视频,是主函数放在前面的原因;主函数最好放在最后,否则出现黄色警告很烦。
#include<stdio.h>
#include<string.h>
struct Student
{ int age;char sex,name[100];
};void InputStudent(struct Student *pstu)
{pstu->age=88;pstu->sex='F';strcpy(pstu->name,"junhao");}int main(void){struct Student st;InputStudent(&st);printf("%d,%c\n",st.age,st.sex);printf("%s",st.name);}
没有警告
当然啦,也可以不利用函数来写
#include<stdio.h>
#include<string.h>
struct Student
{ int age;char sex;
};int main(void){struct Student stu={88,'F'};struct Student *pstu=&stu;pstu->age=88;pstu->sex='F';printf("%d,%c\n",stu.age,stu.sex);}
必须结构体里面有内容才能用指针,否则指向是错的,后赋值不行。