指针笔试题解析
程序员文章站
2024-03-15 19:20:12
...
笔试题1
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int *ptr = (int *)(&a + 1);
//&a取地址数组名–>数组指针->数组指针+1->跳过整个数组
printf( "%d,%d", *(a + 1), *(ptr - 1)); //2 5
return 0;
}
笔试题2
这里告知结构体的大小是20个字节
struct Test
{
int Num;
char *pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
//假设p的值为0x10 0000.
int main()
{
printf("%p\n", p + 0x1);//0x0010 0014
//结构体指针+1,加20个字节,20按十六进制是:14
printf("%p\n", (unsigned long)p + 0x1);//0x0010 0001
//p+0x1 外加一个(unsigned long)类型,强制转换为无符号长整形,
//再加1,结果就是1;(p指针指向空相当于0)按十六进制打印就是0x00100001
printf("%p\n", (unsigned int*)p + 0x1);//0x00100004
//p+0x1外加一个 (unsigned int*)类型,转换为指针,类型是 int 型,
//所以占4个字节,跳过4个字节,按十六进制打印就是0x00100004;
return 0;
}
笔试题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);//4 02000000
//ptr1[-1]= *(ptr1-1),取到元素4
return 0;
}
笔试题4
int main()
{
int a[3][2] = { (0, 1), (2, 3), (4, 5) };
int *p;
p = a[0];
printf("%d", p[0]);//1
//逗号表达式,它的值为最后一个表达式的值
//所以二维数组排列为:
//1 3
//5 0
//0 0
return 0;
}
笔试题5
int main()
{
int a[5][5];
int(*p)[4];
p = a;
printf("%p,%d\n", &a[4][2] - &p[4][2], &a[4][2] - &p[4][2]);
//打印结果为:fffffffc -4
}
笔试题6
int main()
{
int aa[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//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));//10 5
return 0;
}
笔试题7
int main()
{
char *a[] = {"work","at","alibaba"};
char**pa = a;
pa++;
printf("%s\n", *pa); //at
//二级指针指向了指针数组名,++就是指针的指向加一,指到 at
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); //POINT
printf("%s\n", *--*++cpp+3);//ER
printf("%s\n", *cpp[-2]+3);//ST
printf("%s\n", cpp[-1][-1]+1);//EW
return 0;
}