#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int*ptr = (int*)(&a + 1);
printf("%d,%d\n", *(a + 1), *(ptr - 1));
system("pause");
return 0;
}
2.
{
int Num;
char *pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
int main()
{
p = (struct Test*)0x100000;
printf("%p\n", p + 0x1);
printf("%p\n", (unsigned long)p + 0x1);
printf("%p\n", (unsigned int*)p + 0x1);
printf("%p\n", (unsigned char*)p + 0x1);
printf("%p\n", (unsigned char**)p + 0x1);
system("pause");
return 0;
}
首先p是四个字节可写为0x00100000, 加0x1相当于加0x01,指针加一有几个字节取决于指针的类型,因为结构体大小为20个字节,所以十六进制下0x00100014
无符号long相当于0x00100000+0x00 00 00 01
p的类型是指针无符号int*,相当于加四个字节0x00100000+0x00 00 00 04
p的类型是指针无符号char*,相当于加一个字节0x00100000+0x00 00 00 01
p的类型是指针无符号char**,放的是char*类型的地址,相当于加四个字节0x00100000+0x00 00 00 04
3.
int main()
{
int a[4] = { 1, 2, 3, 4 };
int *ptr1 = (int*)(&a + 1);
int *ptr2 = (int*)((int)a + 1);
printf("%x,%x", ptr1[-1], *ptr2);
system("pause");
return 0;
}
本题考到大小端,数组在内存当中的存储,下标是由低地址到高地址的存储
4.
int main(int argc, char * argv[])
{
int a[3][2] = { (0, 1), (2, 3), (4, 5) };
int *p;
p = a[0];
printf("%d", p[0]);
system("pause");
return 0;
}
p[0]可写为*(p+0),表示第0行数组首元素的地址再结引用,所以答案为1,数组为下图所画。
5.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[5][5];
int(*p)[4];
p = (int(*)[4])a;
printf("%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);
system("pause");
return 0;
}
第一个打印地址,第二个打印差值.P[4][2]可写为*(*(p+4)+2)
由图可以看出相减为-4
-4原码1000 0000 0000 0000 0000 0000 0000 0100
反码1111 1111 1111 1111 1111 1111 1111 1011
补码1111 1111 1111 1111 1111 1111 1111 1100
所以应该存储为FF FF FF FC
6.
int main()
{
int aa[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int *ptr1 = (int *)(&aa + 1);
int *ptr2 = (int *)(*(aa + 1));
printf("%d,%d", *(ptr1 - 1), *(ptr2 - 1));
system("pause");
return 0;
}
(aa+1)aa表示数组名,数组名代表数组首元素的地址,加1代表加到第一行的地址,结引用表示这一行本身
所以减一,int*类型减四个字节,所以为5
7.
int main()
{
char *a[] = { "work","at","alibaba" };
char**pa = a;
pa++;
printf("%s\n", *pa);
system("pause");
return 0;
}
数组名代表元素首元素地址,pa代表&a[0],pa++加四个字节,现在保存的是地址,再结引用取到里面的内容
8.
int main()
{
char*c[] = { "ENTER", "NEW", "POINT", "FIRST" };
char**cp[] = { c + 3, c + 2, c + 1, c };
char***cpp = cp;
printf("%s\n", **++cpp);
printf("%s\n", *--*++cpp + 3);
printf("%s\n", *cpp[-2] + 3);
printf("%s\n", cpp[-1][-1] + 1);
system("pause");
}
*cpp[-2]= *( *(cpp-2))
cpp[-1][-1]= * ( * (cpp-1)-1)