C语言结构体(这一篇就够了)

目录

一、结构体变量

1.含义

2.一般形式:

3.结构体类型变量的定义方法

4.结构体的初始化

6.结构体成员的访问:

二、结构体指针:

1.概念

2.定义

3.使用

三、结构体数组:

1.概念

2.定义

3.使用

三、结构体指针数组:

1.概念

2.定义

3.使用


一、结构体变量

1.含义

是一种构造数据类型 ,可以用于存储多个 不同类型的数据 ,在内存中连续存放。

2.一般形式:

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

一个结构体中的一个数据称作结构体的一个成员

比如说描述一个人:基本特征为姓名、性别、身高、体重

(1)定义一个结构体类型:struct  + 结构体名,就以人为例,姓名 :博主 ,性别 :男  ,身高 :180 ,体重 :140 

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={"博主","男",180,140};

注:方法一仅用于读取

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

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

注:使用strcpy时记得加头文件#include


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

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

例如:

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

6.结构体成员的访问:

结构体变量.成员名

例如:

human1.name

human.sex

human.height

human.weight

 Example:

#include
#include
struct human
{
	char name[20];
	char sex[10];
	int height;
	int weight;
};
int main()
{
	struct human human1={"博主","男",180,140};
	//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
#include
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={"博主","男",180,140};
	//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
#include
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={"博主","男",180,140};
	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);
            printf(“…”,human[1].sex);

三、结构体指针数组:

1.概念

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

2.定义

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

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

3.使用

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

定义了一个结构体数组指针
    show(a[1]);打印

我们直接上代码

#include
#include
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={"博主","男",180,140};
	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;
}

你可能感兴趣的:(c语言,c++,算法)