通过malloc函数在调用函数中创建内存供主函数使用
#include<stdio.h>
#include<malloc.h>
//20200201
//跨函数使用内存
struct Student
{
int sid;
int age;
};
struct Student* CreatStudent(void);
ShowStudent(struct Student*);
int main()
{
struct Student *pst;
pst = CreatStudent();
ShowStudent(pst);
return 0;
}
struct Student* CreatStudent()
{
struct Student* pst1 = (struct Student*)malloc(sizeof(struct Student));
pst1->sid = 2020;
pst1->age = 27;
return pst1;
}
ShowStudent(struct Student* pstt)
{
printf(“sid=%d,\nage=%d”,pstt->sid,pstt->age);
}