数组那些不为菜鸟所知的秘密(一)

数组不为菜鸟所知的秘密

#include<iostream>
using namespace std;
//数组指针与指针数组
/*
	int(*p)[5]数组指针本质是指针,指向元素个数为5的数组
	int *p[5]指针数组本质是数组因为[]的优先级比*高p先和[]结合
*/
int main1()
{
	char a[5] = { 'A', 'B', 'C', 'D' };
	char(*p1)[5] = &a;
	cout << p1 << endl;//02FFA54
	cout << p1 + 1 << endl;//02FFA59
	//char(*p2)[10] = &a;//error C2440: “初始化”: 无法从“char (*)[5]”转换为“char (*)[10]
	//char(*p2)[3] = &a;// error C2440 : “初始化” : 无法从“char(*)[5]”转换为“char(*)[3]
	//char(*p2)[5] = a;出错
	//&a代表整个数组的首地址,a代表数组首元素的首地址
	system("pause");
	return 0;
}
int main2()
{
	struct Test
	{
		int Num;
		char *pcName;
		short sDate;
		char cha[2];
		short sBa[4];
	};
	Test *p = (Test*)0;
	cout << p << endl;//0x0
	cout << p + 0x1 << endl;//0x14
	cout << (unsigned long)p + 0x1 << endl;//0x1
	cout << (unsigned int *)p + 0x1 << endl;//0x4
	system("pause");
	return 0;
}
int main()
{
	int a[4] = { 1, 2, 3, 4 };
	int *ptr1 = (int *)(&a + 1);
	int *ptr2 = (int *)((int)a + 1);
	printf("%x\n", ptr1[-1]);//4
	printf("%x\n", *ptr2);//小端模式为0x2000000,大端模式为0x100;///
	system("pause");
	return 0;

}

附判断大小端的代码

int main()
{
	union check
	{
		int i;
		char ch;
	}c;
	c.i = 1;
	if (c.ch == 1)
	{
		cout << "小端" << endl;
	}
	else
	{
		cout << "大端" << endl;
	}
	system("pause");
}

附字节序的理解

http://www.ruanyifeng.com/blog/2016/11/byte-order.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值