赞
踩
结构体可以看做是由程序员自己定义的一种数据类型,结构体中可以包含整型、字符型、指针型变量,也可以包含结构体变量。
标准定义方式:struct + 结构体名称(标签)
1 struct student
2 {
3 char* name;
4 int id;
5 float score;
6 };
宏定义结构体方式:#define 替换后名 struct 结构体名称(标签)
3 #define ST struct student
4 ST //用ST将 struct student替换掉
5 {
6 char* name;
7 int id;
8 float score;
9 };
结构体变量是把结构体当做一种数据类型来定义变量的方式
常规方式:结构体名 + 变量名;
10 struct student
11 {
12 char* name;
13 int id;
14 float score;
15 };
struct student st1,st2;
在定义结构体时直接在后面添加变量名
10 struct student
11 {
12 char* name;
13 int id;
14 float score;
15 } st1, *p;
同样的可以定义结构体时定义结构体指针变量,如上面的 *p 就是结构体指针变量
用typedef方式先将结构体名替换成新的名称,然后用新名称定义变量
10 struct student
11 {
12 char* name;
13 int id;
14 float score;
15 };
typedef struct student ST;
ST st1,st2;
通过typedef将结构体名称替换后,后面都可以直接用新的结构体名去定义变量。同样也可以保留原先的结构体名定义变量的功能。两种方式同时可以存在。
缺省结构结构体名称定义变量的方式是在定义结构体时缺少结构体名(标签),这种方式定义完变量后不能再去定义新的变量,因为没有结构体名(标签),编译器不能识别。这种方式不推荐使用
10 struct
11 {
12 char* name;
13 int id;
14 float score;
15 } st1, *p;
struct student st1 = {"zhangsan",1001,92.5};
struct student st1 = {"zhangsan",1001,92.5}, st3 = {"wangwu",1003,97.5 };
printf("st1.name = %s, st1.id = %d, st1.score = %f\n", st1.name, st1.id, st1.score);
19 struct student st2;
20 st2.name = "lisi";
21 st2.id = 1002;
22 st2.score = 82;
25 printf("st2.name = %s, st2.id = %d, st2.score = %f\n", st2.name, st2.id, st2.score);
结构体变量名类似于整型变量名,变量名中包含有这个结果体中的值,如通过GDB调试可以看到结构体变量名中包含结构体中每个成员的值
struct student st1 = {"zhangsan",1001,92.5};
struct student st5;
st5 = st1;
printf("st1.name = %s, st1.id = %d, st1.score = %f\n", st1.name, st1.id, st1.score); //st1.name = zhangsan, st1.id = 1001, st1.score = 92.500000
printf("st5.name = %s, st5.id = %d, st5.score = %f\n", st5.name, st5.id, st5.score); //st5.name = zhangsan, st5.id = 1001, st5.score = 92.500000
如上可以看到可以 通过结构体整体赋值给另一个结构体。
方式一:
27 stu st3;
28 stu *p = NULL;
29 p = &st3;
30 p->id = 1003; // error p.id
31 p->score = 92.5;
32 p->name = "haha";
33 printf("st3.id = %d, st3.name = %s, st3.score = %f\n",st3.id, st3.name, st3.score);
方式二:
27 struct student st4;
28 struct student *p = NULL;
29 p = &st4;
30 (*p).id = 1004;
31 (*p).score = 87.5;
32 char arr[10] = "wangku";
33 (*p).name = &arr;
34 // strcpy((*p).name, "wangkun"); //编译通过但是报段错误
35 printf("st4.name = %s, st4.id = %d, st4.score = %f\n", st4.name, st4.id, st4.score);
通过结构体类型的指针变量进行间接的对结构体进行赋值。这里需要注意结构体中有字符指针变量,需要先定义一个字符串,然后将字符串地址赋值给结构体中的指针变量。
结构体成员变量中有整型指针时可以先不考虑结构体,或者把结构体和.号当做一个整体对待。如下面代码中 把 st1. 当做一个整体。对于结构体指针 可以把 *p 当做一个整体,*p 用对应的结构体替换。
1 #include <stdio.h> 2 3 struct stu 4 { 5 int x; 6 int *y; 7 }; 8 9 int main() 10 { 11 // 1.常规使用 12 struct stu st1; 13 st1.x = 50; 14 int num = 150; 15 st1.y = # 16 printf("st1.x = %d, *st1.y = %d\n", st1.x, *st1.y); //st1.x = 50, *st1.y = 150 17 18 // 2.结构体数组 19 int data[5] = {10,20,30,40,50}; 20 struct stu arr[3]= {{100,&data[0]}, {200,&data[1]},{300,&data[2]}}; 21 //struct stu arr[3] = {100,&data[0],200,&data[1],300,&data[2]}; //这种赋值方式也可以 22 printf("arr[0].x = %d, *(arr[0].y) = %d\n",arr[0].x,*(arr[0].y)); //arr[0].x = 100, *(arr[0].y) = 10 23 printf("arr[1].x = %d, *(arr[1].y) = %d\n",arr[1].x,*(arr[1].y)); //arr[1].x = 200, *(arr[1].y) = 20 24 25 // 3.结构体指针 26 //成员选择符.号和成员选择符 -> 的优先级高于 取值运算符 * 27 struct stu* p = arr; 28 printf("p->x = %d, (*p).x = %d \n", p->x, (*p).x); //p->x = 100, (*p).x = 100 29 printf("*p->y == *(p->y) = %d,*(*p).y = %d\n", *p->y, *(*p).y); //*p->y == *(p->y) = 10,*(*p).y = 10 30 31 printf("(p+1)->x = %d, (*(p+1)).x = %d \n", (p+1)->x, (*(p+1)).x); //(p+1)->x = 200, (*(p+1)).x = 200 32 printf("*(p+1)->y = %d,*(*(p+1)).y = %d\n", *(p+1)->y, *(*(p+1)).y);//*(p+1)->y = 20,*(*(p+1)).y = 20
需要注意的是:
代码如下:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 struct student 6 { 7 int id; 8 char* name; 9 float score; 10 char nickname[20]; 11 }; 12 typedef struct student stu; 13 14 int main() 15 { 16 stu st1 = {1001,"gld",98.5,"gldnick"}; 17 printf("st1.id = %d, st1.name = %s, st1.score = %f, st1.nickname = %s\n",st1.id, st1.name, st1.score, st1.nick name); 18 //st1.id = 1001, st1.name = gld, st1.score = 98.500000, st1.nickname = gldnick 19 20 stu st2; 21 st2.id = 1002; 22 st2.score = 88; 23 char arr[10] = "wanglin"; 24 //st2.name = arr; 25 st2.name = "linlin"; 26 strcpy(st2.nickname , "linlin_nick"); // error st2.nickname = "linlin_nick" 27 printf("st2.id = %d, st2.name = %s, st2.score = %f, st2.nickname = %s\n",st2.id, st2.name, st2.score, st2.nick name); 28 //st2.id = 1002, st2.name = wanglin, st2.score = 88.000000, st2.nickname = linlin_nick 29 30 stu st3; 31 stu *p = NULL; 32 p = &st3; 33 p->id = 1003; // error p.id 34 p->score = 92.5; 35 p->name = "haha"; 36 strcpy(st3.nickname , "linlin_nick"); // error p->nickname = "haha_nick"; 37 printf("st3.id = %d, st3.name = %s, st3.score = %f, st3.nickname = %s\n",st3.id, st3.name, st3.score, st3.nick name); 38 //st3.id = 1003, st3.name = haha, st3.score = 92.500000, st3.nickname = linlin_nick 39 40 stu st4; 41 p = &st4; 42 (*p).id = 1004; 43 (*p).score = 99.5; 44 (*p).name = "wangwang"; 45 strcpy(st4.nickname , "linlin_nick"); //error (*p).nickname = "wangwang"; 46 printf("st4.id = %d, st4.name = %s, st4.score = %f, st4.nickname = %s\n",st4.id, st4.name, st4.score, st4.nick name); 47 //st4.id = 1004, st4.name = wangwang, st4.score = 99.500000, st4.nickname = linlin_nick 48 }
从上面的代码可以看出,在结构体中含有字符串(char nickname[20])时定定义变量时就赋值操作跟其他结构体相同。定义完后再赋值时需要用strcpy函数对其赋值,通过成员选择符 . 号和成员选择符 -> 赋值都是不允许的。
头文件中需要包含 #include <string.h> 否则会报 :
隐式声明与内建函数‘strcpy’不兼容 故障
代码在4.2节中展示了,可以看出如果在结构体中用字符指针进行字符和字符串的操作比用字符数组要简单。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。