指针的步长

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值