#include
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int* ptr = (int*)(&a + 1);
printf("%d,%d", *(a + 1), *(ptr - 1));
return 0;
}
解析:
笔试题2
#include
struct Test
{
int Num;
char* pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
//假设p 的值为0x100000。 如下表表达式的值分别为多少?
//已知,结构体Test类型的变量大小是20个字节
int main()
{
printf("%p\n", p + 0x1);
printf("%p\n", (unsigned long)p + 0x1);
printf("%p\n", (unsigned int*)p + 0x1);
return 0;
}
解析:
(1)p为结构体指针,+1要跳过1个结构体(20字节)=> 0x100000+1==0x100014
(2)(unsigned long)p =>0x100000为无符号长整型,0x100000+1==0x100001
(3)(unsigned int*)p 为无符号整型指针,+1要跳过1个整型,0x100000+1==0x100004
#include
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);
return 0;
}
解析:
笔试题4
#include
int main()
{
int a[3][2] = { (0, 1), (2, 3), (4, 5) };
int* p;
p = a[0];
printf("%d", p[0]);
return 0;
}
解析:
逗号表达式,就是用逗号隔开的多个表达式。
逗号表达式,从左向右依次执行。整个表达式的结果是最后一个表达式的结果。
笔试题5
#include
int main()
{
int a[5][5];
int(*p)[4];
p = a;
printf("%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);
return 0;
}
解析:
-4的补码的16进制以小端存储是 FFFFFFFC
如果有问题请看http://t.csdn.cn/Vpz8G
笔试题6
#include
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));
return 0;
}
该问题为 笔试题1的变式,解题思路相同
笔试题7
#include
int main()
{
char* a[] = { "work","at","alibaba" };
char** p = a;
p++;
printf("%s\n", *p);
return 0;
}
解析:
笔试题8
#include
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);
return 0;
}
解析:
图8-1
++cpp图(紫色标记为路径 图8-2)
图8-2
*-- * ++cpp + 3图(红色圈圈为路径 图8-3)
图8-3
*cpp[-2] + 3图(粉色圈圈为路径 图8-4)
图8-4
cpp[-1][-1] + 1图(橙色圈圈为路径 图8-5)
图8-5
以上为我个人的小分享,如有问题,欢迎讨论!!!