C语言结构体

结构体

1、简单概述

先了解结构体,可以更加方便的了解java中的类。

结构体:为了模拟一种复杂的事物,由多个简单的数据类型组成。使用结构体来满足我们最基本的需求。

首先看个入门案例:

# include <stdio.h>
// 定义一个结构体
struct Person{
	int id;
	int age;
	double price;
	char sex;
};
int main(void){
	
	// 使用结构体并进行赋值 ,这里的person是结构体变量
	struct Person person = {1,21,22.0,'1'}; 
	printf("person的id值是:%d\n",person.id);
	printf("person的age值是:%d\n",person.age);
	printf("person的price值是:%lf\n",person.price);
	printf("person的sex值是:%c\n",person.sex); 
	return 0;
} 

其实定义结构体有三种方式,但是另外两种不推荐使用,所以这里也就不再来进行介绍了。

但是第一种在使用的时候也有一点不好,就是还得写上struct这个结构体标识。

再来看结构体中引入指针:

# include <stdio.h>
struct Person{
	int id;
	int age;
	double price;
	char sex;
};
int main(void){
	
	// 使用结构体并进行赋值 
	struct Person person = {1,21,22.0,'1'}; 
	// 如果person是一个指针变量,那么给指针赋值就应该是person;但是如果不是一个指针变量,那么就应该是&person
	// 所以person不是一个指针变量,而是一个正常的变量 
	struct Person * pst = &person;
	pst->id = 22;
	pst->age = 23;
	pst->price = 25.0;
	pst->sex = '0';
	printf("person的id值是:%d\n",pst->id);
	printf("person的age值是:%d\n",pst->age);
	printf("person的price值是:%lf\n",pst->price);
	printf("person的sex值是:%c\n",pst->sex); 
	return 0;
}

查看控制台输出:

person的id值是:22
person的age值是:23
person的price值是:25.000000
person的sex值是:0

--------------------------------
Process exited after 0.01567 seconds with return value 0
请按任意键继续. . .

从这里得出来一个结论:如果前面是数据类型+变量名,那么这个变量名不是一个指针;如果声明的时候,是数据类型+*+变量名,那么这个变量是一个指针变量;

但是这里有一个特殊的,那么就是数组。int a[length],这里的a是一个指针变量,这个是比较特殊的。

如果想要使用指针来操作结构体,可以发现和结构体变量的使用有些不同。指针变量使用的是->来操作属性,而结构体变量使用的是.来进行操作。

pst->属性在计算机内部会被转换成(*pst).属性来进行使用。

1.1、总结

struct Person{
	int id;
	int age;
	double price;
	char sex;
};

这里定义了一个结构体,这个结构体由基本数据类型的组成,但是并非要求一定要是基本类型的组成。结构体是一个新的数据类型,是我们可以自己定义的。

作为了一种新的数据结构来使用。

2、简便写法

其实在看到大多数的程序中,看到并非是上面的书写方式,更多的是下面的写法。

# include <stdio.h>
typedef struct Person{
	int id;
	int age;
	double price;
	char sex;
} Person;
int main(void){
	
	Person person ={1,1,2.2F,'1'};
	printf("person中对应的id是:%d ,age是:%d,price是:%lf,sex是:%c",person.id,person.age,person.price,person.sex);	
	
	return 0;
} 

控制台输出:

person中对应的id是:1 ,age是:1,price是:2.200000,sex是:1
--------------------------------
Process exited after 0.02125 seconds with return value 0
请按任意键继续. . .

typedef,类型定义。也就是说这个关键字可以给一种数据类型起一个别名来使用。

使用struct Person这个数据类型,就相当于是使用Person,这里是typedef关键字起作用。

既然可以使用一种数据结构,那么也可以使用指针来进行表示:

# include <stdio.h>
typedef struct Person{
	int id;
	int age;
	double price;
	char sex;
} * PPN;
int main(void){
	
	printf("Person这种数据结构占的字节是:%d\n",sizeof(struct Person));	// 24
	
	return 0;
} 

从这里可以看出来,typedef只是起了一个别名,但是并非是struct Person并不可用。

因为里面的属性占据的字节个数是:4+4+8+1=17,而结构体占据了24个,说明了结构体比里面的基本类型的所占的字节数要大。

使用指针来操作一下这个结构体,这里就是结构体最难的地方了:

# include <stdio.h>
# include <malloc.h>
typedef struct Person{
	int id;
	int age;
	double price;
	char sex;
} Person,* PPN;
int main(void){
	
	printf("Person这种数据结构占的字节是:%d\n",sizeof(struct Person));	
	printf("Person这种数据结构占的字节是:%d\n",sizeof( Person));	
	// 根据以前的方式照葫芦画瓢 
	PPN p = (PPN)malloc(sizeof(Person));
	p->id = 1;	
	p->age = 10;	
	p->price = 22.0F;	
	p->sex = '1';	
	printf("person中的id是:%d,age是:%d,price是:%d,sex是:%d\n",p->id,p->age,p->price,p->sex);
	return 0;
}

查看控制台输出:

Person这种数据结构占的字节是:24
Person这种数据结构占的字节是:24
person中的id是:1,age是:10,price是:0,sex是:49

--------------------------------
Process exited after 0.01804 seconds with return value 0
请按任意键继续. . .
posted @ 2021-08-07 23:36  写的代码很烂  阅读(232)  评论(0编辑  收藏  举报