结构体整理

结构体整理

  • 1. 结构体定义
  • 2. 结构体的使用
  • 3. 段错误的避免
  • 4. 结构体指针
  • 5. 结构体的大小(面试常考)
  • 典例

1. 结构体定义

//定义一个结构体
struct Student		//自定义类型
{
	int score;		//分数 
    char name[128];		//名字 
    int xuehao;
};
//提醒:新手容易忘记在右括号带分号
	//类型    变量名   值
    //int      a=     10;
    struct Student stu1={98,"yjl"};
    printf("结构体中:score=%d\n结构体中:name=%s\n",stu1.score,stu1.name);

	struct Student test;
    test.score=100;
    //test.name="小红",字符串不能单独赋值
    strcpy(test.name,"小红");
    printf("结构体中:score=%d\n结构体中:name=%s\n",test.score,test.name);

2. 结构体的使用

结构体里面都是学过的东西
旧知识,新用法

struct datas
{
	int a;
    char c;
    float f;
    char str[128];
    void (*p)(int a);
    void (*self)(char *name,int num,char sex);
};
//结构体里面都是学过的东西;旧知识,新用法!!!
void fun(int data)
{
	printf("函数:date=%d\n",data);
}
int main()
{
	int a=10;
    printf("a=%d\n",a);
    
    char c='A';
    printf("c=%c\n",c);
    
    float pi=3.14159;
    printf("pi=%.2f\n",pi);
    
    //char *str="hello world";	//初始化可直接用指针指向变量;
    char *str;
    str=(char *)malloc(128);
    str=strcpy(str,"hello world");
    puts(str);
    
    void (*p)(int data)=fun;
    p(10);
    
    puts("===========================");
    struct datas d1;
	d1.a=100;
	d1.c='A';
	d1.f=3.14;
	strcpy(d1.str,"hello");
	d1.p=fun;
	
    printf("a=%d\n",d1.a);
    printf("c=%c\n",d1.c);
    printf("f=%.2f\n",d1.f);
    puts(d1.str);
    d1.p(10);
}

3. 段错误的避免

//内存空间
struct test
{
	//char *p;	//野指针 经典段错误 malloc memset  
    char p[128];
};
int main()
{
	struct test d;
	//d.p=(char *)malloc(128); 	//开辟空间
	//memset(d.p,'\0',128);
    strcpy(d.p,"感觉自己萌萌哒,棒棒哒");	//Linux底下,段错误 
    puts(d.p);
    
	system("pause");
	return 0;
}

4. 结构体指针

注意
1.指针要注意是否为野指针或NULL
2.结构体指针不能用点运算,要用->

struct student
{
	int score;
    char name[128];
};

int main()
{
    struct student stu1;
    stu1.score=100;
	strcpy(stu1.name,"张三");
    printf("test");
    printf("姓名:%s 分数:%d\n",stu1.name,stu1.score);
    
    
    struct student *p;		//野指针
    p=(struct student *)malloc(sizeof(struct student));
    p->score=98;			//结构体指针不能用点运算,要用-> 
	strcpy(p->name,"李四");
	printf("姓名:%s 分数:%d\n",p->name,p->score);
    
    free(p);
    
    p=&stu1;	//指针是存放地址的变量,现在存放的是stu1的地址
    printf("姓名:%s 分数:%d\n",p->name,p->score);

	printf("地址是:%p\n",p++);
	printf("++后地址是:%p\n",p);
}

5. 结构体的大小(面试常考)

参考博文:结构体计算方式
1.偏移量必须是当前成员的大小整数倍
2.结构体大小必须是所有成员大小(数组、结构体除外)的整数倍

//1.偏移量必须是当前成员的大小整数倍
//2.结构体大小必须是所有成员大小(数组、结构体除外)的整数倍
//对齐方式很浪费空间,可是符合计算机的访问规则,提升效率
 
struct s1{		//8 
	char ch1;	//1
	char ch2;	//1
	int i;		//4	      	
}; 

struct s2{		//12
	char ch1;	//1
	int i;		//4
	char ch2;	//1
	
}; 

struct s3{			//20
	char ch;		//1 
	int i;			//4
	char str[12];	//10 
}; 

struct s4{
	char ch;	//1
	int i;		//4
	struct s{	//8	
		char ch1;	//1
		int j;		//4
		double pi;	//8
	};
	float f;		//4
}; 

struct s5{
	char ch;	//1
	int i;		//4 
	union{		//4
		char ch1;
		int j;
	};
};

#pragma pack(4)  //指定向4对齐 最大是8
struct s6{		//20
	char ch;	//1
	int i;		//4
	float f;	//4
	double d;	//8
};

#pragma pack(10)
struct s7{
	char ch;	//1
	int i;		//4
	float f;	//4
	double d;	//8
};
//如果成员的大小超过pack要求,就按pack来对齐,如果没有超过pack要求,就按成员最大大小来对齐
int main() 
{
	printf("int %d\n",sizeof(int));
	printf("char %d\n",sizeof(char));
	printf("float %d\n",sizeof(float));
	printf("double %d\n",sizeof(double));
	
	printf("s1:%d\n",sizeof(s1));
	printf("s2:%d\n",sizeof(s2));
	printf("s3:%d\n",sizeof(s3));
	printf("s4:%d\n",sizeof(s4));
	printf("s5:%d\n",sizeof(s5));
	printf("s6:%d\n",sizeof(s6));
	printf("s7:%d\n",sizeof(s7));
	
	return 0;
}

典例

demo1.c
结构体的封装

#include 
#include 
#define N 3

struct student
{
	int score;
    char *name;
    
};

int main()
{
	struct student stus[N];
    struct student stumax;
    struct student stumin;
    int i;
    
    for(i=0;i<sizeof(stus)/sizeof(stus[0]);i++)		//成绩与姓名的输入 
	{
		stus[i].name=(char*)malloc(128);
        printf("请输入第%d个学生的姓名:",i+1);
		scanf("%s",stus[i].name);
		printf("请输入第%d个学生的成绩:",i+1);
		scanf("%d",&stus[i].score);
    }
	
    for(i=0;i<sizeof(stus)/sizeof(stus[0]);i++)		//每个学生姓名、成绩打印 
	{
		printf("第%d个学生:%s:%d\n",i+1,stus[i].name,stus[i].score);
    }
 
    stumax=stus[0];
    stumin=stus[0];
    
	for(i=0;i<sizeof(stus)/sizeof(stus[0]);i++)		//查找最高分与最低分 
	{
		if(stus[i].score>stumax.score)
			stumax=stus[i];
		if(stus[i].score<stumin.score)
			stumin=stus[i];      
    }
    
    printf("最高分是:%s:%d\n",stumax.name,stumax.score);
    printf("最低分是:%s:%d\n",stumin.name,stumin.score);
     
	system("pause");
	return 0;
}

demo2.c
结构体指针

#include 
#include 
#include 
#define N 3

struct student
{
	int score;
    char *name;
    
};

int main()
{
	int i;
    int len=0;
   //struct student stus[5];
    struct student *p=(struct student*)malloc(5*sizeof(struct student));
    printf("请输入总人数n:");
	scanf("%d",&len);  
    for(i=0;i<len;i++)
    {
		printf("请输入姓名:");
        p->name=(char *)malloc(128);
        //getchar();	过滤回车
        //gets(p->name);
		scanf("%s",p->name);
        printf("请输入成绩:");
        scanf("%d",&(p->score));
        p++;
    }
    
	p=p-len;		//指针挪回头
    for(i=0;i<len;i++)
	{	
        printf("名字:%s 成绩:%d\n",p->name,p->score); 
        p++;     
    }
	system("pause");
	return 0;
}

demo3.c
结构体 指针 函数

#include 
#include 
#include 
#define N 3
//结构体 指针 函数
struct student* initstuscores(int len);		//初始化成绩 
void print(struct student *p,int len);		//打印成绩 
struct student* findmax(struct student*p,int len);	//找最高分 
struct student* findmin(struct student*p,int len);	//找最低分 
float getaverage(struct student *p,int len);		//计算平均分 
int findsome(struct student *p,int len,char *name);	//查找特定某人 
struct student
{
	int score;
    char *name;
};

int main()
{
	int len=0;
	printf("请输入总人数n:");
	scanf("%d",&len);
	
    struct student *pmax=NULL;
    struct student *pmin=NULL;
    
	struct student* pstus=initstuscores(len);
    print(pstus,len);
    pmax=findmax(pstus,len);
    pmin=findmin(pstus,len);
    
    printf("max=%s:%d\nmin=%s:%d\naverage:%.2f\n",pmax->name,pmax->score,pmin->name,pmin->score,getaverage(pstus,len));
    
    if(findsome(pstus,len,"lihua")==1)
		printf("找到了\n");
    else
		printf("查无此人\n");
		
	system("pause");
	return 0;
}


struct student* initstuscores(int len)
{
	int i;
   //struct student stus[5];
    struct student *p=(struct student*)malloc(len*sizeof(struct student));
    for(i=0;i<len;i++)
    {
		printf("请输入姓名:");
        p->name=(char *)malloc(128);
        //getchar();	过滤回车
        //gets(p->name);
		scanf("%s",p->name);
        printf("请输入成绩:");
        scanf("%d",&(p->score));
        p++;
    }
    return p-len;
}

void print(struct student *p,int len)
{
	int i;
    for(i=0;i<len;i++)
	{	
        printf("名字:%s 成绩:%d\n",p->name,p->score); 
        p++;     
    }
}

struct student *findmax(struct student *p,int len)
{
	int i;
    struct student *pmax;
    pmax=p;					//自己写的时候出错  指针一定要赋值 
	for(i=0;i<len;i++)
    {
		if(p->score>pmax->score)
			pmax=p;
		p++;
    }
    return pmax;
}


struct student *findmin(struct student*p,int len)
{
	int i;
    struct student *pmin;
    pmin=p;					//自己写的时候出错  指针一定要赋值 
	for(i=0;i<len;i++)
    {
		if(p->score<pmin->score)
			pmin=p;
		p++;
    }
    return pmin;
}

float getaverage(struct student *p,int len)
{
	int total=0;
    int i;
    
    for(i=0;i<len;i++)
	{
		total+=p->score;
        p++;
	}
    return (float)total/len;
}

int findsome(struct student *p,int len,char *name)
{
	int i;
    for(i=0;i<len;i++)
    {
		if(strcmp(name,p->name)==0)
			return 1;
		p++;
    }
    return -1;
}

你可能感兴趣的:(C语言,c语言)