1.地址
往一个地址当中写入一个数:
*(int *)100 = 25;
指针的指针
int **ptr2ptr;
如何理解更好呢?表达式“**ptr2ptr”的类型是int型
2.头文件中不能定义变量
头文件中定义变量会在文件链接时造成multiple definition错误,
i386-elf-ld -o ./build/kernel.bin -Ttext 0x1000 ./build/kernel_entry.o ./build/kernel.o ./build/screen.o --oformat binary
./build/screen.o:/home/songyj/os_dev/./driver/screen.h:9: multiple definition of `test_str'
./build/kernel.o:/home/songyj/os_dev/driver/screen.h:9: first defined here
应该如下使用:
extern *test_str="THIS IS A TEST MSG!";
3. _ attribute _((packed))
#include <stdio.h>
typedef struct
{
char Data1;
int Data2;
unsigned short Data3;
char Data4;
unsigned short Data5;
}test_struct;
typedef struct
{
char Data1;
int Data2;
unsigned short Data3;
char Data4;
unsigned short Data5;
}__attribute__((packed)) test_struct1;
int main(int argc, char **argv)
{
test_struct a;
test_struct1 b;
a.Data1=1;
a.Data2=2;
a.Data3=3;
a.Data4=0b11111111;
a.Data5=5;
b.Data1=1;
b.Data2=2;
b.Data3=3;
b.Data4=0b11111111;
b.Data5=5;
printf("test_struct size is %d\r\n", sizeof(test_struct));
printf("test_struct1 size is %d\r\n", sizeof(test_struct1));
}
使用gdb调试程序发现 __ atrribute(packed)__ 让结构体中的变量按照实际的大小存储,而默认的时候gcc会插入无效字符对字节进行对齐。
By default, In X86, structures will be padded to 4-byte alignment