C语言结构体 结构体变量 结构体指针 结构体数组 总结(这一个就够用了)

一、结构体变量
1.含义
是一种构造数据类型 ,可以用于存储多个 不同类型的数据 ,在内存中连续存放。
2.一般形式:

struct 结构体名
{
数据类型   成员名1;
数据类型   成员名2;
数据类型   成员名n;
};

一个结构体中的一个数据称作结构体的一个成员
比如说描述一个人:
基本特征为姓名、性别、身高、体重
(1)定义一个结构体类型:
struct + 结构体名,就以人为例,
姓名 :博主 ,
性别 :男 ,
身高 :170 ,
体重 :120

struct human
{
       char name[20];
       char sex[10];
        int height;        
        int weight;
};

3.结构体类型变量的定义方法
(1)先定义结构体类型再定义变量名
这是C语言中定义结构体类型变量最常见的方式

struct 结构体名
{
成员列表;
};
struct 结构体名 变量名;

Example: 定义几个变量:

 struct human
  {
           char name[20];
           char sex[10];          
           int height;         
           int weight;  
 };
 struct human human1,human2; 

(2)在定义类型的同时定义变量
这种形式的定义的一般形式为:

struct 结构体名
{
成员列表;
}变量名;

  Example:
      struct human
        {          
         char name[20];          
         char sex[10];         
          int height;           
          int weight;   
          } human1,human2;   

(3)定义结构体类型别名

1)方法一
typedef struct 结构体名 别名
例如: typedfe struct human human;
2)方法二
在定义结构体类型时就进行别名定义
typedef struct 结构体名
{
成员列表;
}别名;

例如:

 typedef  struct human
   {      
      char name[20];         
      char sex[10];   
      int height;         
      int weight;   } human;

4.结构体的初始化
(1)与其他类型变量一样,也可以给结构体的每个成员赋初值,这称为结构体的初始化。一种是在定义结构体变量时进行初始化,语法格式如下:

struct 结构体名 变量名={初始数据表};

例如:
1)结构体成员的读操作

struct human human1={“博主”,“男”,170,120};

注:方法一仅用于读取

2)字符串拷贝 ,结构体成员的写操作

strcpy(human1.name," 博主");
strcpy(human1.sex = “男”);

注:使用strcpy时记得加头文件#include<string.h>

(2)另一种是在定义结构体类型时进行结构体变量的初始化。

struct 结构体名{
成员列表;
}变量名={初始数据表};

例如:

struct human{      
      char name[20];
      char sex[10];        
       int height;          
       int weight;  
} human1={"博主","男",170,120};

6.结构体成员的访问:

结构体变量.成员名

例如:

human1.name
human.sex
human.height
human.weight

Example:

#include<stdio.h>
#include<string.h>
struct human
{
	char name[20];
	char sex[10];
	int height;
	int weight;
};
int main()
{
	struct human human1={"博主","男",170,120};
	//strcpy(human1.name,"李");
	//strcpy(human1.sex,"女");
	printf("name=%s\n",human1.name);
	printf("sex=%s\n",human1.sex);
	printf("height=%d\n",human1.height);
	printf("weight=%d\n",human1.weight);
	return 0;
}

二、结构体指针:
1.概念

    可以设定一个指针变量用来指向一个结

构体变量。此时该指针变量的值是结构体变
量的 起始地址,该指针称为结构体指针。
结构体指针与前面介绍的各种指针变量
在特性和方法上是相同的。与前述相同,在
程序中结构体指针也是通过访问目标运算“*”
访问它的对象。

2.定义

struct 结构体名 *结构指针名;

对于上一节中定义的结构体类型
struct human,
可以说明使用这种结构体类型的结构指针如下:struct human *p;

3.使用

结构体指针 -> 成员名;

例如,结构体指针p指向的结构体变量中的成员name可以表示如下:

(*p).name 或 p->name

#include<stdio.h>
#include<string.h>
struct human
{
	char name[20];
	char sex[10];
	int height;
	int weight;
};
void show(struct human *p)
{
	printf("name=%s\n",p->name);
	printf("sex=%s\n",p->sex);
	printf("height=%d\n",p->height);
	printf("weight=%d\n",p->weight);
}
int main()
{
	struct human human1={"博主","男",170,120};
	//strcpy(human1.name,"李");
	//strcpy(human1.sex,"女");
	//printf("name=%s\n",human1.name);
	//printf("sex=%s\n",human1.sex);
	//printf("height=%d\n",human1.height);
	//printf("weight=%d\n",human1.weight);
	show(&human1);
	return 0;
}

三、结构体数组:
1.概念

一个数组 其元素是 结构体

2.定义
定义结构体数组的方法和定义结构体变量的方法相仿,只需说明其为数组即可。
还是以上面定义的人的结构体为例
如:

struct human arr[2]={human,human2};

#include<stdio.h>
#include<string.h>
struct human
{
	char name[20];
	char sex[10];
	int height;
	int weight;
};
void show(struct human *p)
{
	printf("name=%s\n",p->name);
	printf("sex=%s\n",p->sex);
	printf("height=%d\n",p->height);
	printf("weight=%d\n",p->weight);
}
int main()
{
	struct human human1={"博主","男",170,120};
	struct human human2={"小七","女",160,80};
	struct human arr[2]={human1,human2};
	//当我们要读取第一个成员时
	show(&arr[0]);
	//strcpy(human1.name,"李");
	//strcpy(human1.sex,"女");
	//printf("name=%s\n",human1.name);
	//printf("sex=%s\n",human1.sex);
	//printf("height=%d\n",human1.height);
	//printf("weight=%d\n",human1.weight);
	//show(&human1);
	return 0;
}


3.使用
以上面定义的结构体数组arr[2]为例说明对结构体数组的引用:
1)引用某一元素中的成员。
若要引用数组第二个元素的name成员,则可写为:

              arr[1].name

2)可以将一个结构体数组元素值赋给同一结构体类型的数组中的另一个元素,或赋给同一类型的变量。
如:

         struct  human[3],student1;

现在定义了一个结构体类型的数组,它有3个元素,又定义了一个结构体类型变量student1,则下面的赋值是合法的。

  student1=human[0];
      human[0]=human[1];            
      human[1]=student1;     

3)不能把结构体数组元素作为一个整体直接进行输入输出。
如:

printf(“…”,human[0]);

scanf(“…”,&human[0]);
都是错误的。
只能以单个成员为对象进行输入输出,
如:
scanf(“…”,human[0].name);
scanf(“…”,&human[1].height);
printf(“…”,human[0].weight);\n
printf(“…”,human[1].sex);

三、结构体指针数组:
1.概念

一个数组 其元素是 结构体指针

2.定义
同上结构体指针一致,还是以上面定义好的human结构体给大家举个例子
如:

struct human *arr[2]={&human,&human2};

3.使用

struct human *a[2]={&human1,&human2};
定义了一个结构体数组指针
show(a[1]);打印

#include<stdio.h>
#include<string.h>
struct human
{
	char name[20];
	char sex[10];
	int height;
	int weight;
};
void show(struct human *p)
{
	printf("name=%s\n",p->name);
	printf("sex=%s\n",p->sex);
	printf("height=%d\n",p->height);
	printf("weight=%d\n",p->weight);
}
int main()
{
	struct human human1={"博主","男",170,120};
	struct human human2={"小七","女",160,80};
	struct human arr[2]={human1,human2};
	struct human *a[2]={&human1,&human2};
	//当我们要读取第一个成员时
	show(&arr[0]);
	show(a[1]);
	//strcpy(human1.name,"李");
	//strcpy(human1.sex,"女");
	//printf("name=%s\n",human1.name);
	//printf("sex=%s\n",human1.sex);
	//printf("height=%d\n",human1.height);
	//printf("weight=%d\n",human1.weight);
	//show(&human1);
	return 0;
}

  • 12
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值