数组不为菜鸟所知的秘密
#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