#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h> //offsetof头文件
//指针的步长
//1、指针变量+1后跳跃的字节数
void test01()
{
char* p = NULL;
//int a = 1000;
//p = &a;
printf("%d\n", p);
printf("%d\n", p+1);
}
//2、在解引用的时候,取出的字节数
void test02()
{
char buf[1024] = { 0 };
int a = 10005;
memcpy(buf+1, &a, sizeof(a));
char* p = buf;
printf("%d\n", *(int *)(p+1));
}
//指针步长练习
struct Person
{
char a;
int b;
char buf[1024];
int d;
};
void test03()
{
struct Person p = { 'a',10,"hello world",1000 };
//获取结构体p中的d属性偏移量是多少?
printf("d的偏移量为:%d\n", offsetof(struct Person, d));
printf("d的值:%d\n", p.d);
printf("d的值为:%d\n", *(int*)((char *)&p + offsetof(struct Person, d)));
}
int main(int argc, char* argv[])
{
test03();
system("pause");
return 0;
}