四、跨函数使用内存讲解及其示例
#include <stdio.h>
#include <malloc.h>
struct Student
{
int age;
int sid;
};
struct Student * CreateStudent();
void ShowStudent(struct Student *);
int main(void)
{
struct Student * ps;
ps = CreateStudent();
ShowStudent(ps);
return 0;
}
//因为返回的是指针p,它的类型为struct Student
*,所以方法前面要加这个
struct Student * CreateStudent(void)
{
struct Student * p = (struct Student *)malloc(sizeof(struct
Student));
//给指针p分配了struct Student需要占用的字节大小
//相当于new了一个p
p->age = 99;
p->sid = 88;
return p;
}
void ShowStudent(struct Student * pst)
{
printf(“%d %d”, pst->age, pst->sid);
}
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!