结构体相关内容

一、定义1个结构体,为其赋值,打印
1.写1个简单的结构体

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
	struct student 
	{
		int score ;
		char name[128] ;	
	};
	struct student stu1 = {99,"rising"};
	printf("score = %d,name = %s",stu1.score,stu1.name);
	return 0;
}

在这里插入图片描述

2.为结构体变量赋值时,字符串必须用字符串的函数来填充,不能直接赋值

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
	struct student 
	{
		int score ;
		char name[128] ;	
	};
	struct student stu1 = {99,"rising"};
	struct student stu2 = {66,"dog"};
	printf("score = %d,name = %s",stu1.score,stu1.name);
	printf("score = %d,name = %s",stu2.score,stu2.name);
	stu2.score =88;
	stu2.name = "dog1";
	return 0;
}
stu2.name = "dog1";

像这样写是错误的,会报错
在这里插入图片描述

应该把 stu2.name = “dog1”;写成strcpy(stu2.name,“dog1”);

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
	struct student 
	{
		int score ;
		char name[128] ;	
	};
	struct student stu1 = {99,"rising"};
	struct student stu2 = {66,"dog"};
	printf("score = %d,name = %s\n",stu1.score,stu1.name);
	printf("score = %d,name = %s\n",stu2.score,stu2.name);
	stu2.score =88;
	//stu2.name = "dog1";
	strcpy(stu2.name,"dog1");
	printf("score = %d,name = %s\n",stu2.score,stu2.name);
	return 0;
}

在这里插入图片描述以字符数组设置字符串时,
字符串只有在初始化时,才可以直接赋值,其他时候必须strcpy
以字符串指针设置字符串时,可以不适用strcpy

像这样定义时赋初值,是可以直接赋值的

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
	char a[128] = "hello";
	puts(a);
	return 0;
}

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
	char *p;//这是字符串指针,它就可以不用strcpy
	p = "yrx";
	puts(p);
	p = "hello";
	puts(p);
	return 0;
}

而,先定义,再赋值是不行的

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
	char a[128];
	a[128] = "hello";
	puts(a);
	return 0;
}

在这里插入图片描述
对于字符串,一般还是需要先申请空间的

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
	struct student 
	{
		int score ;
		char *name ;	
	};

	struct student stu2 ;
	stu2.name = (char *)malloc(128);
	memset(stu2.name,'\0',128);
	strcpy(stu2.name,"yrx");
	stu2.score = 100;
	printf("name:%s,score:%d",stu2.name,stu2.score);
	return 0;
}

在这里插入图片描述
3.结构体数组
数组的每个元素存放的都是1个结构体(实现的效果类似多维数组)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
	int i;
	struct student 
	{
		int score ;
		char *name ;	
	};

	struct student arr[2] ;
	printf("结构体数组的每一个元素都存放着1个结构体的内容\n");


	for(i = 0;i<sizeof(arr)/sizeof(arr[0]);i++)
	{
		printf("第%d个学生的姓名:\n",i);
		arr[i].name = (char *)malloc(128);
		scanf_s("%s",arr[i].name);

		printf("第%d个学生的分数:\n",i);
		scanf_s("%d",&arr[i].score);
	}


    	for(i = 0;i<sizeof(arr)/sizeof(arr[0]);i++)
	{
	  	printf("arr[%d].name:%s,arr[%d].score:%d\n",i,arr[i].name,i,arr[i].score);  
	}


	return 0;
}

在这里插入图片描述
排序找出结构体数组中的最高分

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
	int i;
	struct student 
	{
		int score ;
		char *name ;	
	};

	struct student arr[3] ;
	struct student maxScore ;
	printf("结构体数组的每一个元素都存放着1个结构体的内容\n");


	for(i = 0;i<sizeof(arr)/sizeof(arr[0]);i++)
	{
		printf("第%d个学生的姓名:\n",i);
		arr[i].name = (char *)malloc(128);
		scanf_s("%s",arr[i].name);

		printf("第%d个学生的分数:\n",i);
		scanf_s("%d",&arr[i].score);
	}


    	for(i = 0;i<sizeof(arr)/sizeof(arr[0]);i++)
	{
	  	printf("arr[%d].name:%s,arr[%d].score:%d\n",i,arr[i].name,i,arr[i].score);  
	}

    maxScore.score = arr[0].score;
    for(i = 0;i<sizeof(arr)/sizeof(arr[0]);i++)
	{
	  	if(maxScore.score < arr[i].score) 
	  	maxScore.score = arr[i].score;
	}
	printf("maxscore = %d",maxScore.score);
	return 0;
}

在这里插入图片描述
4.结构体指针
使用结构体指针时,不能用点运算符访问结构体中的变量,而应该使用->

结构体指针初次定义时是要开辟一块新的空间的,这个空间大小和结构体模板一样大,z这个空间的地址是单独存在的。
当把一个具体的结构体变量的地址赋值给结构体指针时,结构体指针会指向这个地址。具体情况看下面程序就一目了然了。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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

int main()
{

	struct student stu1  = {"rising",100};
	struct student *pstu;//定义一个结构体指针(也指针)
	pstu =(struct student *) malloc(sizeof(struct student));//在内存上申请1块空间,这个空间和这个结构体模板一样大
	pstu->name = "张三";
	pstu->score = 23;
	printf("stu1.name =   %s  ,stu1.score = %d\n",stu1.name,stu1.score);
	printf("&(stu1.name) = %p,&(stu1.score) = %p\n",&stu1.name,&stu1.score);
	printf("pstu->name =   %s  ,pstu-score = %d\n",pstu->name,pstu->score);
	printf("&(pstu->name) = %p,&(pstu->score) = %p\n",&pstu->name,&pstu->score);

	strcpy(pstu->name,"李四");//在结构体指针所指空间上进行赋值,指针变量存放的是用malloc开辟出来的空间的地址
	printf("pstu->name =   %s  ,pstu-score = %d\n",pstu->name,pstu->score);
	printf("&(pstu->name) = %p,&(pstu->score) = %p\n",&pstu->name,&pstu->score);

	strcpy(pstu->name,stu1.name);
	pstu->score = stu1.score;//在结构体指针所指空间上进行赋值,指针变量存放的是用malloc开辟出来的空间的地址
	printf("pstu->name =   %s  ,pstu-score = %d\n",pstu->name,pstu->score);
	printf("&(pstu->name) = %p,&(pstu->score) = %p\n",&pstu->name,&pstu->score);

	pstu = &stu1;//让结构体指针指向具体的结构体变量,指针变量存放的是stu1的地址
	printf("pstu->name =   %s  ,pstu-score = %d\n",pstu->name,pstu->score);
	printf("&(pstu->name) = %p,&(pstu->score) = %p\n",&pstu->name,&pstu->score);
	return 0;
}

在这里插入图片描述
结构体指针移动1次的偏移量是结构体模板的大小

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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

int main()
{

	struct student stu1  = {"rising",100};
	struct student *pstu;//定义一个结构体指针(也指针)
	pstu =(struct student *) malloc(sizeof(struct student));//在内存上申请1块空间,这个空间和这个结构体模板一样大

	pstu = &stu1;//让结构体指针指向具体的结构体变量

	printf("pstu:%d\n",(pstu));
	printf("pstu++:%d\n",(++pstu));

	printf("结构体的大小:%d字节\n",sizeof(struct student));

	return 0;
}

在这里插入图片描述
5.结构体指针在和结构体数组配合使用的时候,肯定会面临指针滑动。
记得要在指针滑动结束后,重新指向结构体数组。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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

int main()
{
	int i;
	struct student arr[5];//定义1个结构体数组
	struct student *pstu;
	pstu = arr;//这里让结构体指针指向结构体数组首地址,所以就不要为结构体指针开辟空间了
	printf("接下来要输入结构体数组的具体内容\n");
	for(i = 0;i<sizeof(arr)/sizeof(arr[0]);i++)
	{
		printf("输入第%d个学生的姓名\n",i);
		pstu->name = (char*)malloc(128);//申请一块空间
		scanf("%s",pstu->name);
		printf("输入第%d个学生的分数\n",i);
		scanf("%d",&(pstu->score) );
		pstu++;//这里自增是因为:最初指针pstu指向数组的元素0,自增后才能依次指向元素1,元素2,,,
		
	}
	
	pstu = arr;//前面的for循环指向玩后,pstu已经指向了数组末尾并再次自增1次,已经不在数组的元素0的位置了
				//重新让指针指向数组开头,再开始打印
	for(i = 0;i<sizeof(arr)/sizeof(arr[0]);i++)
	{
		printf("name:%s,score:%d\n",pstu->name,pstu->score);//pstu->name本身就是字符串的指针,打印字符串,填地址(指针)
															//pstu->score是变量,打印整型数,填变量名
		pstu++;
	}
	return 0;
}

在这里插入图片描述6.
(1)利用结构体指针、结构体指针函数实现查找最高分的效果

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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


struct student * init_student(int len)//返回值为结构体指针,所以函数类型定义为结构体指针类型
{
    int i;
    struct student *p = (struct student *)malloc(len * sizeof(struct student));
   
   for(i = 0;i < len;i++)
   {
    printf("输入学生姓名\n");
    p->name = malloc(128);
    scanf("%s",p->name);
    printf("输入分数\n");
    scanf("%d",&(p->score));
    p++;
   }
   return p-len;
}

void print_message(struct student *pstu,int len)
{
	int i;
	for(i = 0;i<len;i++)
	{
		printf("name : %s , score : %d\n",pstu->name,pstu->score);
		pstu++;
	
	}

}

int find_max(struct student *pstu,int len)
{
    int max = 0;
    int i;
	for(i = 0;i<len;i++)
	{
        if(pstu->score > max)
        max = pstu->score;
        pstu++;
	
	}
    
    return max;
}
int main()
{
	int len = 0;
	int ret_max=0;
	printf("输入学生的数量:\n");
	scanf("%d",&len);
	struct student *p=init_student(len);
    print_message(p,len);
    ret_max = find_max(p,len);
    printf("max = %d",ret_max);
	return 0;
}

在这里插入图片描述这里有1个疑惑,有1个结构体指针,如果它通过自增移动了3次,那么它返回到最初的位置只需要p= p-3;为什么??
我的理解是:p-3中的3不是整数,而是3个结构体大小的值,这样理解p-3,它的值就对的上了。
为什么这样想呢?
p是指针,是struct student * p型的数据。
p-3 ==p-3*sizeof(struct student )
(2)也可以这样写

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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


struct student * init_student(int len)//返回值为结构体指针,所以函数类型定义为结构体指针类型
{
    int i;
    struct student *p = (struct student *)malloc(len * sizeof(struct student));
   
   for(i = 0;i < len;i++)
   {
    printf("输入学生姓名\n");
    p->name = malloc(128);
    scanf("%s",p->name);
    printf("输入分数\n");
    scanf("%d",&(p->score));
    p++;
   }
   return p-len;
}

void print_message(struct student *pstu,int len)
{
	int i;
	for(i = 0;i<len;i++)
	{
		printf("name : %s , score : %d\n",pstu->name,pstu->score);
		pstu++;
	
	}

}

struct student * find_max(struct student *pstu,int len)
{
    struct student *pmax;
        int i;
    pmax = pstu;

	for(i = 0;i<len;i++)
	{
        if(pstu->score > pmax->score)
        pmax->score = pstu->score;
        pstu++;
	
	}
    
    return pmax;
}
int main()
{
	int len = 0;

	printf("输入学生的数量:\n");
	scanf("%d",&len);
	struct student *p=init_student(len);
	struct student * ret_max;
    print_message(p,len);
    ret_max = find_max(p,len);
    printf("max = %d",ret_max->score);
	return 0;
}

在这里插入图片描述
(3)根据姓名来查找某一个同学是否存在(字符串方式)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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


struct student * init_student(int len)//返回值为结构体指针,所以函数类型定义为结构体指针类型
{
    int i;
    struct student *p = (struct student *)malloc(len * sizeof(struct student));
   
   for(i = 0;i < len;i++)
   {
    printf("输入学生姓名\n");
    p->name = malloc(128);
    scanf("%s",p->name);
    printf("输入分数\n");
    scanf("%d",&(p->score));
    p++;
   }
   return p-len;
}

void print_message(struct student *pstu,int len)
{
	int i;
	for(i = 0;i<len;i++)
	{
		printf("name : %s , score : %d\n",pstu->name,pstu->score);
		pstu++;
	
	}

}

struct student * find_max(struct student *pstu,int len)
{
    struct student *pmax;
        int i;
    pmax = pstu;

	for(i = 0;i<len;i++)
	{
        if(pstu->score > pmax->score)
        pmax->score = pstu->score;
        pstu++;
	
	}
    
    return pmax;
}


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




int main()
{
	int len = 0;

	printf("输入学生的数量:\n");
	scanf("%d",&len);
	struct student *p=init_student(len);
	struct student * ret_max;
    print_message(p,len);
    ret_max = find_max(p,len);
    printf("max = %d\n",ret_max->score);
    if(find_someone_for_name(p,len,"zhangsan") == -1)
    printf("no,张三不在\n");
    else
    printf("yes,张三在\n");
	return 0;
}

在这里插入图片描述在这里插入图片描述
二、结构体大小怎么计算

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值