笔试题1:
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int* ptr = (int*)(&a+1);
printf( "%d,%d", *(a+1), *(ptr-1));
return 0;
}//程序的结果是什么?
(a+1)中a是数组首元素的地址,+1得到第二个元素的地址,(a+1)得到2
int ptr = (int)(&a+1);
笔试题2:
struct Test
{
int Num;
char *pcName;
short sDate;
char cha[2];
short sBa[4];
}p = (struct Test)0x100000;
//假设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;
}
printf("%p\n", p+0x1);
结构体指针+1跳过一个结构体的大小的长度,即0x100000+20得到0x10014
printf("%p\n", (unsigned long)p+0x1);
先强制类型转换为无符号长整型后+1,即0x100000+1得到0x100001
printf("%p\n", (unsigned int*)p+0x1);
先强制类型转换为无符号整型指针后+1,即0x100000+4得到0x100004,要注意指针加一和整形加一的区别
笔试题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);
return 0;
}
笔试题4:
int main()
{
int a3 = { (0, 1), (2, 3), (4, 5) };
int*p;
p=a[0];
printf( "%d", p[0]);
return 0;
}
int a3 = { (0, 1), (2, 3), (4, 5) };
注意这是一段逗号表达式,所以二维数组初始化为:
a[0]是第一行的数组名,a[0]表示首元素的地址,即a0的地址,p[0]——>(p+0)——>p
笔试题5:
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;
}
分析如下图:
结果:
笔试题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));
return 0;
}
笔试题7:
int main()
{
char*a[] = {"work","at","alibaba"};
char**pa=a;
pa++;
printf("%s\n", *pa);
return 0;
}
笔试题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);
return 0;
}
char* c[] = { "ENTER","NEW","POINT","FIRST" };
char** cp[] = { c + 3,c + 2,c + 1,c };
char*** cpp = cp;
先画出三个指针间初始的关系图:
1)
printf("%s\n", **++cpp);
cpp先自增,解引用得到c+2,再解引用得到元素P的地址,向后打印得到POINT
2)
printf("%s\n", -- ++cpp + 3);
cpp先++,解引用得到c+1,然后c+1自减得到c,再解引用ENTER的首元素地址,首元素地址+3得到元素E的地址,向后打印得到ER
3)
printf("%s\n", cpp[-1][-1] + 1);
4)
printf("%s\n", *cpp[-2] + 3);
cpp[-2] + 3——>(*(cpp-2))+3,类似3)可得打印结果为ST