✨✨✨✨如果文章对你有帮助记得点赞收藏关注哦!!✨✨✨✨
文章目录
- ✨✨✨✨如果文章对你有帮助记得点赞收藏关注哦!!✨✨✨✨
- 一维数组练习题:
- 字符数组练习题:
- 字符指针练习题:
- 二维数组练习题:
- 编程题:
- 完结
你真的学会指针了吗?几组练习题,能全答对超99%初学者,务必思考后再看答案🧐:
做题之前必须明白以下规则
提示:
1.数组名一般情况下都是代表首地址,不过有两种特殊情况
(1)数组名单独出现在sizeof内部
(2)&数组名
2.指针在32位平台下的大小是4个字节,在64位平台下是8个字节。(下面答案内的4/8就是值32位平台和64位平台分别为4/8)
3.sizeof不关心内部是什么值,只关心返回结果的类型,比如
#include <stdio.h> int main() {int a = 0;int b = 0;printf("%d",sizeof(b=a+1)); //4printf("%d",b); //0 可以看到b并没有被赋值,因为sizeof不关心内部运算只在意返回类型,整形a+整形1赋值给整形b结果肯定为整形,直接就返回4 }
一维数组练习题:
//一维数组
#include <stdio.h>
int main()
{int a[] = {1,2,3,4};printf("%d\n",sizeof(a));printf("%d\n",sizeof(a+0));printf("%d\n",sizeof(*a));printf("%d\n",sizeof(a+1));printf("%d\n",sizeof(a[1]));printf("%d\n",sizeof(&a));printf("%d\n",sizeof(*&a));printf("%d\n",sizeof(&a+1));printf("%d\n",sizeof(&a[0]));printf("%d\n",sizeof(&a[0]+1));
}
答案
字符数组练习题:
//字符数组
#inlcude <stdio.h>
int main()
{char arr[] = {'a','b','c','d','e','f'};printf("%d\n", sizeof(arr));printf("%d\n", sizeof(arr+0));printf("%d\n", sizeof(*arr));printf("%d\n", sizeof(arr[1]));printf("%d\n", sizeof(&arr));printf("%d\n", sizeof(&arr+1));printf("%d\n", sizeof(&arr[0]+1));printf("%d\n", strlen(arr));printf("%d\n", strlen(arr+0));printf("%d\n", strlen(*arr));printf("%d\n", strlen(arr[1]));printf("%d\n", strlen(&arr)); printf("%d\n", strlen(&arr+1));printf("%d\n", strlen(&arr[0]+1));
}
答案
#inlcude <stdio.h>
int main()
{char arr[] = "abcdef";printf("%d\n", sizeof(arr));printf("%d\n", sizeof(arr+0));printf("%d\n", sizeof(*arr));printf("%d\n", sizeof(arr[1]));printf("%d\n", sizeof(&arr));printf("%d\n", sizeof(&arr+1));printf("%d\n", sizeof(&arr[0]+1));printf("%d\n", strlen(arr));printf("%d\n", strlen(arr+0));printf("%d\n", strlen(*arr));printf("%d\n", strlen(arr[1]));printf("%d\n", strlen(&arr));printf("%d\n", strlen(&arr+1));printf("%d\n", strlen(&arr[0]+1));
}
答案
字符指针练习题:
#inlcude <stdio.h>
int main()
{char *p = "abcdef";printf("%d\n", sizeof(p));printf("%d\n", sizeof(p+1));printf("%d\n", sizeof(*p));printf("%d\n", sizeof(p[0]));printf("%d\n", sizeof(&p));printf("%d\n", sizeof(&p+1));printf("%d\n", sizeof(&p[0]+1));printf("%d\n", strlen(p));printf("%d\n", strlen(p+1));printf("%d\n", strlen(*p));printf("%d\n", strlen(p[0]));printf("%d\n", strlen(&p));printf("%d\n", strlen(&p+1));printf("%d\n", strlen(&p[0]+1));
}
答案
二维数组练习题:
提示
//二维数组
#inlcude <stdio.h>
int main()
{int a[3][4] = {0}; printf("%d\n",sizeof(a)); printf("%d\n",sizeof(a[0][0]));printf("%d\n",sizeof(a[0]));printf("%d\n",sizeof(a[0]+1));printf("%d\n",sizeof(*(a[0]+1)));printf("%d\n",sizeof(a+1));printf("%d\n",sizeof(*(a+1)));printf("%d\n",sizeof(&a[0]+1));printf("%d\n",sizeof(*(&a[0]+1)));printf("%d\n",sizeof(*a));printf("%d\n",sizeof(a[3]));
}
答案
编程题:
问:下列程序输出什么?
#inlcude <stdio.h>
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,5
a为首元素地址,+1为第二个元素的地址,解引用就是2
&a+1加步长为整个数组,强转成int*赋值给ptr,ptr-1步长就是int *为四个字节,就是5的地址,解引用就是5
//由于还没学习结构体,这里告知结构体的大小是20个字节
#inlcude <stdio.h>
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;
}
答案:
2,5
#inlcude <stdio.h>
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;
}
答案:
什么是小端存储?链接: 大小端字节序讲解。
#include <stdio.h>
int main()
{int a[3][2] = { (0, 1), (2, 3), (4, 5) };int *p;p = a[0];printf( "%d", p[0]);return 0;
}
答案:
#inlcude <stdio.h>
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;
}
答案:
#inlcude <stdio.h>
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;
}
答案:
#include <stdio.h>
int main()
{char *a[] = {"work","at","alibaba"};char**pa = a;pa++;printf("%s\n", *pa);return 0;
}
答案:
#inlcude <stdio.h>
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;
}
答案:
(1) POINT
(2) ER
(3) ST
(4)EW
完结
创作不易,还请各位小伙伴多多点赞👍关注✨收藏⭐