指针的步长

 ̄綄美尐妖づ 提交于 2020-01-24 16:28:44
#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;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!