注意:以下都是32位的系统
一、结构体定义
第一个问题:下面两个
struct human{
char sex;
int length;
char name[3];
};
struct human{
char name[3];
char sex;
int length;
};
这两个结构体是否相同?
答案: 不一样
原因: 根据c语言结构体的机制,我们为结构体变量分配一个连续空间时,总是以结构体中最大字节长度类型的整数倍分配。
第一个的内存分布图:
sex length name[3]
四个字节 四个字节 四个字节
总占据 12个字节
第二个的内存分布图
name[3] sex length
3个字节 1个字节 4个字节
总占据 8个字节
代码验证:
1 #include <stdio.h>
2
3 struct student {
4 char sex;
5 int score;
6 char name[3];
7 //char sex;
8 //int score;
9 //student *i;
10 };
11 int main()
12 {
13 printf("%d\n",sizeof(struct student));
14 return 0;
15 }
执行结果:
book@www.100ask.org:~/ablerry$ vi 1.c
book@www.100ask.org:~/ablerry$ gcc -o 8 1.c
1.c: In function ‘main’:
1.c:13:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("%d\n",sizeof(struct student));
^
book@www.100ask.org:~/ablerry$ ./8
12
1 #include <stdio.h>
2
3 struct student {
4 char name[3];
5 char sex;
6 int score;
7 //student *i;
8 };
9 int main()
10 {
11 printf("%d\n",sizeof(struct student));
12 return 0;
13 }
1.c: In function ‘main’:
1.c:13:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("%d\n",sizeof(struct student));
^
book@www.100ask.org:~/ablerry$ ./7
bash: ./7: No such file or directory
book@www.100ask.org:~/ablerry$ ./8
8
第二种情况 其中带结构体自身的指针 内存大小该如何算
1 #include <stdio.h>
2
3 typedef struct student {
4 char sex;
5 //int score;
6 char name[3];
7 //char sex;
8 int score;
9 }STUDENT;
10
11 struct num {
12 char sex;
13 char name[3];
14 int score;
15 STUDENT *p;
16 };
17 int main()
18 {
19 printf("STUDENT SIZE: %d\n",sizeof(STUDENT));
20 printf("num SIZE: %d\n",sizeof(struct num));
21 return 0;
22 }
执行结果:
book@www.100ask.org:~/ablerry$ gcc -o 8 1.c
1.c: In function ‘main’:
1.c:19:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("STUDENT SIZE: %d\n",sizeof(STUDENT));
^
1.c:20:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("num SIZE: %d\n",sizeof(struct num));
^
book@www.100ask.org:~/ablerry$ ./8
STUDENT SIZE: 8
num SIZE: 16
第三种情况结构体中带指针函数 内存该如何计算
1 #include <stdio.h>
2
3 typedef struct student {
4 char sex;
5 //int score;
6 char name[3];
7 //char sex;
8 int score;
9 }STUDENT;
10
11 struct num {
12 char sex;
13 char name[3];
14 int score;
15 STUDENT *p;
16 STUDENT (*fun)();
17 };
18 int main()
19 {
20 printf("STUDENT SIZE: %d\n",sizeof(STUDENT));
21 printf("num SIZE: %d\n",sizeof(struct num));
22 return 0;
23 }
执行结果:
book@www.100ask.org:~/ablerry$ gcc -o 8 1.c
1.c: In function ‘main’:
1.c:20:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("STUDENT SIZE: %d\n",sizeof(STUDENT));
^
1.c:21:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("num SIZE: %d\n",sizeof(struct num));
^
book@www.100ask.org:~/ablerry$ ./8
STUDENT SIZE: 8
num SIZE: 24
综合三种情况基本结论如下:
1.根据c语言结构体的机制,我们为结构体变量分配一个连续空间时,总是以结构体中最大字节长度类型的整数倍分配。
2.在考虑到第一点的情况下,结构体的大小总是取决于结构体里面的数据类型大小
二、结构体的访问方式
第一种 普通的结构体类型
typdef struct num{
char a;
int b;
} NUM;
num *p;
请问如何访问结构体指针p中 a 和 b 两个成员
指针直接访问:
p->a;
p->b;
指针间接访问: //为什么称之为间接访问 * 是间接访问符
(*p).a
(*p).b
代码验证如下:
1 #include <stdio.h>
2
3 typedef struct num {
4 char a;
5 int b;
6 } NUM;
7
8 NUM *p;
9 NUM data1 ={'A',23};
10 int main()
11 {
12 p= &data1;
13 printf("p->a: \t %d\n",p->a);
14 printf("p->b: \t %d\n",p->b);
15
16 printf("(*p).a: \t %d\n",(*p).a);
17 printf("(*p).b: \t %d\n",(*p).b);
18 return 0;
19
20
21 }
执行结果如下:
book@www.100ask.org:~/ablerry$ gcc -o c_struct 2.c
book@www.100ask.org:~/ablerry$ ./c_struct
p->a: 65
p->b: 23
(*p).a: 65
(*p).b: 23
第二种 带指针的结构体
typedef struct num {
· char *c;
char *a;
int b;
} NUM;
num *p;
举例如下:
1 #include <stdio.h>
2
3 typedef struct num {
4 int *p;
5 char *a;
6 int b;
7 } NUM;
8
9 NUM *p1;
10
11 NUM data1 ={&5,'A',23};
12 int main()
13 {
14 //int *b = 10;
15 //p1 = (struct)
16 //printf("%d\n",*b);
17 p1= &data1;
18 printf("p1->(*p): \t %d \n",p1->p);
19 printf("p1->a: \t %d\n",p1->a);
20 printf("p1->b: \t %d\n",p1->b);
21
22 printf("(*p1).a: \t %d\n",(*p1).a);
23 printf("(*p1).b: \t %d\n",(*p1).b);
24 return 0;
25 }
~
这个访问结构体成员体变量的 方法就是l两种方式 p1->a (*p1).a