关于指针的面试题,指向字符串和字符数组的单指针,二级指针,三级指针的使用。

    xiaoxiao2022-07-13  151

    int a[3][4] = { 0 }; printf("%d\n", sizeof(a));//48 printf("%d\n", sizeof(a[0][0]));//4 printf("%d\n", sizeof(a[0]));//16 printf("%d\n", sizeof(a[0] + 1));//4 地址 printf("%d\n", sizeof(*(a[0] + 1)));//4 int printf("%d\n", sizeof(a + 1));//地址 printf("%d\n", sizeof(*(a + 1)));//a[1]16 printf("%d\n", sizeof(&a[0] + 1));//4 &a[0]][1] printf("%d\n", sizeof(*(&a[0] + 1)));// a[0][1] 16 对第二行解引用 printf("%d\n", sizeof(*a));//16 a[0] printf("%d\n", sizeof(a[3]));//16

    int a[5] = { 1, 2, 3, 4, 5 }; int *ptr = (int *)(&a + 1);//跳到整个数组后面去的地址 printf("%d,%d", *(a + 1), *(ptr - 1));//2 5

    int a = 6; int *p = &a; printf("%p\n", p); printf("%p\n", p+0x1);//p的基类型是int +1 就是加一个int所占的字节空间 printf("%p\n", (unsigned long)p+0x1);//转成整数+1 printf("%p\n", (short *)p+0x1);//short 占两个字节加两个字节

     

    const char *c[] = { "ENTER","NEW","POINT","FIRST" }; const char**cp[] = { (c + 3),c + 2,c + 1,c }; const 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);//*(*(cpp-1)-1)+1

     

     这道题应该注意的是对指针cpp的操作应该区别

    *cpp++  先执行解引用操作,待所有操作完成后,要执行下一行的时候,cpp的值才会改变。

    ++cpp 先执行++操作变量的值发生变化

    cpp+1 只对指针的执行改变,变量的值并不会发生变化。

    const char *a[] = { "work","at","alibaba" }; const char**pa = a; pa++; printf("%s\n", *pa);//at

     

    int a[3][2] = { (0, 1), (2, 3), (4, 5) };//{1,3,5,0,0,0} int *p; p = a[0]; printf("%d", p[0]);//1 int a[4] = { 1, 2, 3, 4 }; int *ptr1 = (int *)(&a + 1); int *ptr2 = (int *)((int)a + 1); printf("%x,%x\n", ptr1[-1], *ptr2);//4,2000000 printf("%p",a); //在内存中1的存法是 00000001 00000000 00000000 00000000 //2 00000010 00000000 00000000 00000000 //(int)a+1 就是整数+1,指针会后移一个字节,结合内存的小端存储方式应该读出的是 //00000010 00000000 00000000 00000000 //%x 十六进制 2000000

     这道题涉及到了内存的存储方式,在一般的windows操作系统win32平台下,是小端字节序,即低位存到高地址上,读取也是从低位开始读。小端模式是颠倒书写顺序进行存储的

    const char *str[] = { "welcome","to","fortemedia","nanjing" }; const char **p = str + 1; str[0] = (*p++) + 2; str[1] = *(p + 1); str[2] = p[1] + 3; str[3] = p[0] + (str[2] - str[1]); printf("%s\n", str[0]); printf("%s\n", str[1]); printf("%s\n", str[2]); printf("%s\n", str[3]); printf("%d\n", str[2] - str[1]);

    这道题的坑在于 str[2] - str[1] 求的是 从str[]存储的位置开始到‘\0’结束。

     

     

     

    对字符数组变量名的含义和用法不熟悉的朋友可以参见我的另几篇博客。

    一维数组数组名含义解析,数组指针的使用

    https://blog.csdn.net/weixin_43447989/article/details/90346271

    一维数组传参、二维数组传参、一级指针传参、二级指针传参

      https://blog.csdn.net/weixin_43447989/article/details/90347625

    (*( void (*) ( ) ) 0  ) ()的含义,二维数组数组名的含义和用法 *(*(a+i)+j)的含义,*(a+1)+3的含义,*(a+1+1)的含义https://blog.csdn.net/weixin_43447989/article/details/90380906

    最新回复(0)