#include<stdio.h>
typedef struct Student
{
int age;
char name[100];
}Stu;
void InputStudent(Stu pstu)
{
pstu.age=10;
}
int main()
{
Stu st;
InputStudent(st);
printf("%d",st.age);
return 0;
}
输出垃圾值,在函数中无法修改st中的值
使用指针:直接将结构体地址传入InputStudent,对st地址操作
#include<stdio.h>
typedef struct Student
{
int age;
char name[100];
}Stu;
void InputStudent(Stu* pstu)
{
pstu->age=10;
//(*pstu).age=10;
//pstu.age=10;
}
int main()
{
Stu st;
InputStudent(&st);
printf("%d",st.age);
return 0;
}
输出正常值