参看 C语言部分
#include <stdio.h>
#include <malloc.h>
#include <string.h>
/*
跨函数使用内存
1, 在A函数中 使用了一块内存
2, 在A函数结束后, B函数仍然可以使用 这块内存
本例:
1, 在createStudent函数中, 创建了结构体
2, 在printStudent函数中, 使用了该结构体
*/
struct Student
{
int id;
char name[100];
int age;
};
// 创建一个结构体, 并返回其首地址
struct Student * createStudent(void);
// 打印结构体
void printStudent(struct Student * pSt);
int main(void)
{
struct Student * pSt = NULL;
pSt = createStudent();
printStudent(pSt);
return 0;
}
struct Student * createStudent()
{
struct Student * pSt = (struct Student *) malloc( sizeof(struct Student) );
pSt->id = 123;
strcpy(pSt->name, "张三");
pSt->age = 20;
return pSt;
}
void printStudent(struct Student * pSt)
{
printf("%d, %s, %d\n", pSt->id, pSt->name, pSt->age);
return;
}