结构体的使用

结构体struct

1.结构体的基本用法

1.1 定义

结构体是由一批数据组合而成的结构型数据。组成结构型数据的每个数据称为结构型数据的“成员” ,其描述了一块内存区间的大小及解释意义。

1.2 定义格式

struct 结构体名
{
数据类型 成员变量1;
数据类型 成员变量2;
数据类型 成员变量3;

};

举例:构造一个新的数据类型叫student,用来描述学生。

struct student
{
char name[32];
int age;
float score;
};

1.3 结构体变量

1.3.1 概念

通过结构体数据类型定义的变量

1.3.2 格式

struct 结构体名 变量名;

1.3.3 定义结构体变量

(1)先定义结构体,再定义结构体变量。

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

struct 结构体名 变量名;
例如:

struct student
{
char name[32];
int age;
float score;
};
struct student s1;

(2)定义结构体的同时,定义结构体变量。

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

例如:

struct person
{
char name[32];
int age;
} per1;

也可以缺省结构体名定义结构体变量:

struct
{
成员变量;
} 变量名;

1.3.4 结构体变量赋值

先定义一个结构体:

struct student
{
int id;
char name[32];
int age;
};

(1)定义结构体变量时直接用花括号赋值:

struct student stu1 ={1,“xiaofang”, 18};

(2)定义结构体变量时直接用点等法赋值:

struct student stu2 ={
.id = 2,
.name = “xiaoming”,
.age = 42
};

(3)定义结构体变量时未初始化,需要挨个单独赋值:

struct student stu3;
stu3.id = 3;
stu3.age = 69;
strcpy(stu3.name,“laowang”); 注意字符串以这种方式赋值的话,需要用strcpy

1.3.5 访问结构体成员变量

通过.访问: 结构体变量名.结构体成员变量名。

struct student stu4;
scanf(“%d %s %d”,&stu4.id,stu4.name,&stu4.age);
printf(“%d %s %d\n”,stu4.id,stu4.name,stu4.age);

练习:创建一个名为student的结构体,包含姓名,学号,班级, 从终端输入学生的信息并打印。

#include <stdio.h>
#include <string.h>
struct student
{
    char name[32];
    int id;
    int class;
};

int main()
{
   struct student stu1;
   scanf("%s %d %d",stu1.name,&stu1.id,&stu1.class);
   printf("%s %d %d\n",stu1.name,stu1.id,stu1.class);
}

1.3.6 重定义 typedef

给语句另起一个名字
例如:
typedef int size_t;
此时int a; 也可以写成 size_t a;
typedef int int_pointer;*
int a;
此时int* p=&a; 可以写成 int_pointer p=&a;

(1)定义结构体的同时重定义

#include <stdio.h>
#include <string.h>
typedef struct flower
{
    char type[32];
    int size;
    char color[32];
} flo;

int main()
{
    flo f1 = {"rose",10,"red"};
    printf("%s %d %s\n",f1.type,f1.size,f1.color);
}

(2)先定义结构体,再进行重定义。

struct animal
{
    char type[32];
    char class[32];
    int wight;
};
typedef struct animal ANIMAL;
int main()
{
    ANIMAL an1 = {"dog","buru",200};
    printf("%s %s %d\n",an1.type,an1.class,an1.wight);
}

练习:创建一个描述手机的结构体叫phone, 包含品牌,型号,颜色,价格。从终端输入你自己手机的信息并打印。

typedef struct phone
{
    char brand[32];
    char typde[32];
    char color[32];
    int price;
} pho;
int main()
{
    pho ph1;
    scanf("%s %s %s %d", ph1.brand, ph1.typde, ph1.color, &ph1.price);
    printf("%s %s %s %d\n", ph1.brand, ph1.typde, ph1.color, ph1.price);
    return 0;
}

2.结构体数组

2.1 概念

结构体类型相同的变量组成的数组

2.2 定义格式

(1)定义结构体同时定义结构体数组

struct student
{
int id;
int age;
float score;
} stu[5];

(2)先定义结构体,再定义结构体数组。

struct student
{
int id;
int age;
float score;
};
struct student stu[5];

2.3 初始化和赋值

(1)定义结构体数组的同时赋值

#include <stdio.h>
#include <string.h>
struct hero
{
    char name[32];
    char postion[32];
    char skill1[32];
    char skill2[32];
    char skill3[32];
    char skill4[32];
    int price;
};
int main()
{
    struct hero h[2] =
        {
            {"ali","fashi","1","2","3","4",4300},
            {"shitou","tanke","1","2","3","4",3150}
        };

    printf("%s %s %s %s %s %s %d\n", h[1].name, h[1].postion, h[1].skill1, h[1].skill2, h[1].skill3, h[1].skill4, h[1].price);
    return 0;
}

(2)先定义结构体数组,在对数组内每个元素分别赋值。

#include <stdio.h>
#include <string.h>
struct hero
{
    char name[32];
    char postion[32];
    char skill1[32];
    char skill2[32];
    char skill3[32];
    char skill4[32];
    int price;
};
int main()
{
    struct hero he[2];
    strcpy(he[0].name,"namei");
    strcpy(he[0].postion,"fashi");
    he[0].price = 6300;
    printf("%s %s %d\n", he[0].name, he[0].postion,he[0].price);
    return 0;
}

2.4 结构体数组输入输出(通过循环)

#include <stdio.h>
#include <string.h>
struct hero
{
    char name[32];
    char postion[32];
    int life;
    int magic;
    float speed;
    int price;
};
int main()
{
    struct hero h[5] =
        {
            {"ali", "magicer", 596, 481, 53.04, 6300},
            {"gailun", "tank", 700, 0, 55.04, 450},
            {"jee", "ad", 596, 200, 55.04, 6300},
            {"cat", "helfer", 400, 400, 45, 6300},
            {"ez", "adc", 550, 450, 53.04, 6300},
        };
    for (int i = 0; i < 5; i++)
    {
        printf("%s %s %d %d %f %d\n",h[i].name,h[i].postion,h[i].life,h[i].magic,h[i].speed
,h[i].price);
    }
    return 0;
}

2.5 结构体大小

(1)结构体类型大小*元素个数
(2)用sizeof(struct 结构体名) 或者sizeof(结构体变量名)
例如接着上面的练习:
printf(“struct hero:%d %d\n”,sizeof(struct hero),sizeof(h));//80 400

3.结构体大小以及对齐原则

3.1 结构体大小用sizeof计算

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

struct st
{
    char a;
    char b;
    int c;
};

struct s
{
    char a;
    int b;
    char c;
};
int main()
{
    printf("%d\n",sizeof(struct st)); //8
    printf("%d\n",sizeof(struct s));  //12
    return 0;
}

3.2 字节对齐原则

(1)第一个成员在相对于结构体变量起始位置偏移量为0的地址处
(通俗点来说,就是第一个成员变量的地址与结构体起始位置的地址是相同的)如下图所示:
在这里插入图片描述

(2)用结构体成员里面最大的数据类型的大小和字节进行比较,然后按照字节数小的为单位开辟空间。例如上图例子,每个成员都要给他以4字节为单位开辟空间。

用结构体成员里面最大的数据类型的大小和字节进行比较,然后按照字节数小的为单位开辟空间。 通俗来说,结构体将结构体内最大的数据类型与4字节比较,以小的为基准。结构体是以4字节为单位进行存储的,相同类型的可以合并4字节存储起来,如

char a
char b
int c

是两个char占用4字节,加Int是八字节
如果5个char占用8字节。

char a[29]
char b[29]
int c

以4字节为基准,两个字符数组60字节。29+29=58<60(60/4=12)再加4,总的是64字节。

3.3 节省空间原则

为了减少空间浪费,把占用空间小的成员集中到一起。
在这里插入图片描述

4.结构体指针

4.1 概念

指向结构体变量的指针

4.2 定义格式

*struct 结构体名 结构体指针名;
例如:

struct student
{
int id;
char name[32];
}s1,s2;
struct work
{
int id;
int age;
}w1,w2;
struct student *sp = &s1;
//struct student *p=&w1; //错误,结构体类型不同

4.3 结构体指针成员变量赋值

格式:指针变量名->成员变量名
或者:(*指针变量名).成员变量名
注意 在对数组中元素赋值是,第二种要用:指针变量名[i].成员变量名,即p[i].name,

#include <stdio.h>
#include <string.h>
struct student
{
    int id;
    char name[32];
}s1,s2;
int main()
{
    struct student *sp = &s1;
    strcpy(sp->name,"xiaoming");
    sp->id=123;
    printf("%s %d\n",s1.name,s1.id);
    printf("%s %d\n",sp->name,sp->id);

    return 0;
}

结构体指针大小:4字节,因为本质还是指针。

4.4 结构体内结构体

如果结构体内成员本身属于另一种结构体,得用若干个成员运算符一级级找到最低级的成员变量。

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

struct work
{
    int ip;
};

struct student
{
    int id;
    int age;
    struct work w1;
} stu1, stu2;

int main()
{
    stu1.w1.ip = 2;
    stu1.id = 1;
    stu1.age = 20;
    printf("%d %d %d\n", stu1.w1.ip, stu1.id, stu1.age);
    return 0;
}

结构体总结:
1.不能把结构体类型变量作为整体引用,只能对结构体内变量中的各个成员分别引用。
2.如果成员本身属于另一种结构体类型,要用若干个成员运算符.一级级找到最低级成员变量。
3.可以把成员变量当成普通变量运算
4.在数组中,成员之间不能彼此赋值,但是结构体变量可以互相赋值。

练习:创建一个结构体数组,数组名为book,结构体成员包含编号,书名,售价(数据类型自己设定)。写一个函数,包含两个形参,分别接收结构体数组的首地址和一个指定的售价,函数的功能为打印结构体数组中售价大于指定售价的书的信息。
#include <stdio.h>
#include <string.h>

typedef struct book
{
int number;
char name[32];
int price;
} BOOK;
BOOK b[5]={
{1,“santi”,25},
{2,“hongloumeng”,45},
{3,“jinpingmei”,50},
{4,“xiyouji”,87},
{5,“sanguo”,67}
};

void book_Pri(BOOK *p, int n)
{
for(int i=0;i<5;i++)
{
if(p->price >= n)
printf(“%d %s %d\n”,p->number,p->name,p->price);
p++;
}
}
int main()
{
book_Pri(b,50);
return 0;
}