栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言结构体应用与不同方式的分配空间

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言结构体应用与不同方式的分配空间

#include
#include

#define elemtype1 int
#define elemtype2 char

typedef struct student{
elemtype1 id;
elemtype2 name[11];
elemtype2 sex;
}stu;   //结构体别名

typedef struct teacher{
elemtype1 *id;
elemtype2 *name[11];
elemtype2 *sex;
}teacher;   //结构体别名

typedef struct brother{
elemtype1 id;
elemtype2 name[11];
elemtype2 sex;
}brother;   //结构体别名


void PrintStu(stu *student)
{
    printf("stu1 id is :%dn",student->id);
    printf("stu1 name is :%sn",student->name);
    printf("stu1 sex is :%cn",student->sex);
  
}

void PrintStu1(teacher *student)
{
    printf("stu1 id is :%dn",student->id);
    printf("stu1 name is :%sn",student->name);
    printf("stu1 sex is :%cn",student->sex);
  
}

void PrintStu2(brother *student)
{
    printf("stu1 id is :%dn",student->id);
    printf("stu1 name is :%sn",student->name);
    printf("stu1 sex is :%cn",student->sex);
  
}

void printf3n()
{
	printf("n");
    printf("n");
    printf("n");
}

elemtype1 main()
{
	elemtype1 i;
	elemtype1 b=2022;
	elemtype2 c='m';
	elemtype2 a[11]="chenguanye";
	stu stu1;
	teacher teacher1;
	brother brother1;
    printf("*****************开始********************n");
    printf3n();
    
    teacher1.id=2022;
    strcpy(teacher1.name, "chenguanye");
    teacher1.sex='m';
    PrintStu1(&teacher1);
    printf("sizeof of teacher1 is     :%dn",sizeof(teacher1));
    printf("sizeof of teacher1.id is  :%dn",sizeof(teacher1.id));
    printf("sizeof of teacher1.name is:%dn",sizeof(teacher1.name));
    printf("sizeof of teacher1.sex is :%dn",sizeof(teacher1.sex));
    
    
    
    teacher *ss = (teacher*)malloc(sizeof(teacher)); //申请堆内存
    ss->id=2022;
    strcpy(ss->name, "chenguanye");
    ss->sex='m';
    printf3n();
    PrintStu1(ss);
    printf("sizeof of ss is      :%dn",sizeof(ss));
    printf("sizeof of ss->id is  :%dn",sizeof(ss->id));
    printf("sizeof of ss->name is:%dn",sizeof(ss->name));
    printf("sizeof of ss->sex is :%dn",sizeof(ss->sex));
 
 
    
	stu1.id=2022;
    strcpy(stu1.name, "chenguanye");
	stu1.sex='m';		
	printf3n();	
	PrintStu(&stu1);	
	printf("sizeof of stu1 is     :%dn",sizeof(stu1));
	printf("sizeof of stu1.id is  :%dn",sizeof(stu1.id));
	printf("sizeof of stu1.name is:%dn",sizeof(stu1.name));
	printf("sizeof of stu1.sex is :%dn",sizeof(stu1.sex));
	
	
    
    //结构体指针
    stu *s = (stu*)malloc(sizeof(stu)); //申请堆内存
    s->id = 2022;
    strcpy(s->name, "chenguanye");
    s->sex = 'm';
    printf3n();
    PrintStu(s);
    printf("sizeof of s is      :%dn",sizeof(s));
    printf("sizeof of s->id is  :%dn",sizeof(s->id));
    printf("sizeof of s->name is:%dn",sizeof(s->name));
    printf("sizeof of s->sex is :%dn",sizeof(s->sex));
    
    
    
    brother1.id=b;
    for(i=0;i<10;i++)
    {
    	brother1.name[i]=a[i];
	}
    brother1.sex=c;
    printf3n();
    PrintStu(&brother1);
	printf("sizeof of brother1 is     :%dn",sizeof(brother1));
	printf("sizeof of brother1.id is  :%dn",sizeof(brother1.id));
	printf("sizeof of brother1.name is:%dn",sizeof(brother1.name));
	printf("sizeof of brother1.sex is :%dn",sizeof(brother1.sex));
	
	
    //结构体指针
    stu *sss = (brother*)malloc(sizeof(brother)); //申请堆内存
    sss->id = b;
    for(i=0;i<10;i++)
    {
       sss->name[i]=a[i];   	
	} 
    sss->sex = c;
    printf3n();
    PrintStu(sss);
    printf("sizeof of sss is      :%dn",sizeof(sss));
    printf("sizeof of sss->id is  :%dn",sizeof(sss->id));
    printf("sizeof of sss->name is:%dn",sizeof(sss->name));
    printf("sizeof of sss->sex is :%dn",sizeof(sss->sex));
    
    
           
    printf3n();  
    printf("*****************结束********************n");
}



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/913788.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号