线
int PileInts[1024];
创建一个由 1024 个整数组成的数组对象。可以使用变量名PileInts访问该对象
线
char *Pile = (char *)PileInts;
创建一个char 指针 对象并使其指向数组对象的第一个字符。使用变量名Pile 访问char 指针对象。
char 指针Pile 可用于访问PileInts 的各个字节。示例:
#include <stdio.h>
int main(void) {
int PileInts[1024];
char *Pile = (char *)PileInts;
PileInts[0] = 1;
PileInts[1] = 2;
// Print the bytes/chars of the first two ints of PileInts
for (unsigned i= 0; i < (2 * sizeof PileInts[0]); ++i)
{
printf("0x%02x\n", *Pile); // Print what Pile points to
++Pile; // Increment Pile so it points to the next byte/char
}
return 0;
}
可能的输出:
0x01
0x00
0x00
0x00
0x02
0x00
0x00
0x00
注意:由于字节序和/或整数大小不同,输出可能因系统而异。
如果你想查看Pile的值,即它所指向的地址,你可以修改如下代码:
#include <stdio.h>
int main(void) {
int PileInts[1024];
char *Pile = (char *)PileInts;
PileInts[0] = 1;
PileInts[1] = 2;
for (unsigned i= 0; i < (2 * sizeof PileInts[0]); ++i)
{
printf("Pile points to addresss %p where the value 0x%02x is stored\n",
(void*)Pile, *Pile);
++Pile;
}
return 0;
}
可能的输出:
Pile points to addresss 0x7ffe5860b8e0 where the value 0x01 is stored
Pile points to addresss 0x7ffe5860b8e1 where the value 0x00 is stored
Pile points to addresss 0x7ffe5860b8e2 where the value 0x00 is stored
Pile points to addresss 0x7ffe5860b8e3 where the value 0x00 is stored
Pile points to addresss 0x7ffe5860b8e4 where the value 0x02 is stored
Pile points to addresss 0x7ffe5860b8e5 where the value 0x00 is stored
Pile points to addresss 0x7ffe5860b8e6 where the value 0x00 is stored
Pile points to addresss 0x7ffe5860b8e7 where the value 0x00 is stored