00struct.c
#include <stdio.h>
// 编译器对齐, 让每个成员对齐(CPU -- RAM)访问边界.
struct s {
int a;
int b;
char str[6];
};
int main(void)
{
struct s obj = {
.a = 123,
.b = 88,
.str = "hello",
};
printf("sizeof obj = %u\n", sizeof obj);
// . 是最高优先级的运算符
printf("content of obj: %d, %d, %s\n", obj.a, obj.b, obj.str);
printf("-------------------------\n");
printf("&obj = %p\n", &obj);
printf("&obj.a = %p, &obj.b = %p\n", &obj.a, &obj.b);
printf("obj.str = %p, &obj.str = %p\n", obj.str, &obj.str);
printf(" obj.str + 1 = %p\n", obj.str + 1);
printf("&obj.str + 1 = %p\n", &obj.str + 1); // 地址+5
printf("=========================\n");
struct s *pobj = NULL;
pobj = &obj;
printf("sizeof pobj = %u\n", sizeof pobj);
// -> 和 . 优先级相同.
printf("content of obj: %d, %d, %s\n", pobj->a, pobj->b, pobj->str);
printf("-------------------------\n");
printf("&pobj = %p, pobj = %p, &obj = %p\n", &pobj, pobj, &obj);
printf("&pobj->a = %p, &pobj->b = %p\n", &pobj->a, &pobj->b);
printf("pobj->str = %p, &pobj->str = %p\n", pobj->str, &pobj->str);
printf(" pobj->str + 1 = %p\n", pobj->str + 1);
printf("&pobj->str + 1 = %p\n", &pobj->str + 1);
return 0;
}
运行结果:
sizeof obj = 16
content of obj: 123, 88, hello
-------------------------
&obj = 0xbfb5337c
&obj.a = 0xbfb5337c, &obj.b = 0xbfb53380
obj.str = 0xbfb53384, &obj.str = 0xbfb53384
obj.str + 1 = 0xbfb53385
&obj.str + 1 = 0xbfb5338a
=========================
sizeof pobj = 4
content of obj: 123, 88, hello
-------------------------
&pobj = 0xbfb53378, pobj = 0xbfb5337c, &obj = 0xbfb5337c
&pobj->a = 0xbfb5337c, &pobj->b = 0xbfb53380
pobj->str = 0xbfb53384, &pobj->str = 0xbfb53384
pobj->str + 1 = 0xbfb53385
&pobj->str + 1 = 0xbfb5338a
01struct.c
#include <stdio.h>
#include <stdlib.h>
struct s {
int a;
int b;
char str[6];
};
int main(void)
{
struct s obj = {
.a = 123,
.b = 88,
.str = "hello",
};
printf("obj's content: %d %d %s\n", obj.a, obj.b, obj.str);
printf("---------------------------\n");
struct s obj1;
obj1 = obj; // 结构体可以直接赋值
printf("obj1's content: %d %d %s\n", obj1.a, obj1.b, obj1.str);
printf("---------------------------\n");
struct s *pobj = NULL;
pobj = malloc(sizeof(struct s));
if (NULL == pobj)
return 0;
*pobj = obj;
printf("(*pobj)'s content: %d %d %s\n", pobj->a, pobj->b, pobj->str);
printf("(*pobj)'s content: %d %d %s\n",(*pobj).a, (*pobj).b, (*pobj).str); // 这两种访问方式都可
free(pobj);
return 0;
}
运行结果:
obj's content: 123 88 hello
---------------------------
obj1's content: 123 88 hello
---------------------------
(*pobj)'s content: 123 88 hello
(*pobj)'s content: 123 88 hello
02bitfield.c
#include <stdio.h>
// 位域. bit field.
struct s {
int field1:4;
int field2:20;
int field3:16;
};
int main(void)
{
struct s obj;
printf("sizeof obj = %u\n", sizeof obj);
obj.field1 = 0xf;
obj.field2 = 0xff;
obj.field3 = 1234;
printf("--------------------------\n");
// 4 位, 最高位(1)扩展.
printf("obj.field1 = %#x\n", obj.field1);
// 20 位, 0xff 是8位, 最高位(0)扩展.
printf("obj.field2 = %#x\n", obj.field2);
printf("obj.field3 = %d\n", obj.field3);
return 0;
}
运行结果:
sizeof obj = 8
--------------------------
obj.field1 = 0xffffffff
obj.field2 = 0xff
obj.field3 = 1234
03union.c
#include <stdio.h>
// 联合体/共用体
union u {
int a;
char ch;
double d;
};
int main(void)
{
printf("sizeof union u = %u\n", sizeof(union u));
union u u;
u.a = 65;
printf("u.a = %d, u.ch = %c, u.d = %lf\n", u.a, u.ch, u.d);
u.ch = 'a';
printf("u.a = %d, u.ch = %c, u.d = %lf\n", u.a, u.ch, u.d);
u.d = 3.14;
printf("u.a = %d, u.ch = %c, u.d = %lf\n", u.a, u.ch, u.d);
return 0;
}
运行结果:
sizeof union u = 8
u.a = 65, u.ch = A, u.d = -0.000000
u.a = 97, u.ch = a, u.d = -0.000000
u.a = 1374389535, u.ch = , u.d = 3.140000
04enum.c
#include <stdio.h>
// 枚举, 第一个成员默认值是0
enum color_type {
COLOR_START,
BLACK,
WHITE,
BLUE = 5,
GREEN,
RED = 8,
YELLOW,
OGRANGE,
COLOR_END,
};
int main(void)
{
enum color_type color;
color = COLOR_START;
printf("COLOR_START = %d\n", color);
color = BLACK;
printf("BLACK = %d\n", color);
color = GREEN;
printf("GREEN = %d\n", color);
color = COLOR_END;
printf("COLOR_END = %d\n", color);
return 0;
}
运行结果:
COLOR_START = 0
BLACK = 1
GREEN = 6
COLOR_END = 11