#include<stdio.h>
#include<stdlib.h>
struct Info
{
int age;
int num;
};
void print(struct Info* p)
{
printf("%d %d \n",p->age,p->num);
}
struct Info* creatInfo(int age,int num)
{
struct Info* info3=(struct Info*)malloc(sizeof(struct Info));
info3->age=age;
info3->num=num;
return info3;
}
int main()
{
struct Info*info=NULL;
struct Info info1;
struct Info*info2=NULL;
info=&info1;
info->age=18;
info->num=1001;
printf("%d %d \n",info->age,info->num);
printf("%d %d \n",info1.age,info1.num);
info2=(struct Info*)malloc(sizeof(struct Info));
if(info2==NULL)
{
printf("申请内存失败");
return 0;
}
info2->age=21;
info2->num=1002;
printf("%d %d \n",info2->age,info2->num);
free(info2);
info2=NULL;
info2=creatInfo(29,1004);
print(info2);
free(info2);
system("pause");
return 0;
}
|