本文将以练习题的方式着重对指针进行理解与对做题时出现的错误进行解析
做这些问题之前,需要知道的是,sizeof内部单独放一个数组名, 代表整个数组, &数组名, 此时也是取出整个数组的地址。
首先请看以下例题:>
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));
return 0;
}
(下面是解析,做完题再看!!!)
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(a));
此处sizeof(a)中单独放置了一个数组名,在这种情况下,数组名代表整个数组,计算的是整个数组所占内存的空间大小,如下图所示
因此,本题的答案为4 * 4, 即为16byte.
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(a + 0));
此处sizeof(a+0),这一个+0,又把a变得不孤单了,此处sizeof(a + 0),数组名a并没有单独放在sizeof内部,因此表示的是数组首元素的地址,即
因此,你a+0后仍然是一个指向1的地址,因此,结果为地址的大小,即4/8byte!
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(*a));
这里中sizeof仍然没有单独放置数组名,因此,数组名a仍然表示数组首元素的地址,对该地址解引用,找到的是我们的数字1,即
因此,即求得是整型的大小,因此,本题答案为4byte,即int类型所占空间的大小。
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(a + 1));
本题与sizeof(a + 0)那道题很类似,a+1其实也就是将首元素的地址加上1,跳过一个整型,来到数组的第二个元素,即
因此,结果也就为地址的大小,是4/8byte
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(a[1]));
sizeof(a[1]),就是在访问下标为1的元素嘛,在这里下标为1的元素是整型数字2,因此,求得也就是int类型所占内存空间大小,结果为4
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(&a));
还记得开头那句话吗?(忘记的回去重看!😂),&a此处的a代表的是整个数组,即取出整个数组的地址。即
说了这么多,但它本质上还是一个指针啊!,指针的大小即地址的大小,因此结果为4/8byte
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(*&a));
第二组例题:>
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));
(依然是一点点分割线)
解析:>
第三组例题:>
char arr[] = {'a','b','c','d','e','f'};
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))
这题要注意arr中有无’\0’字符.
(分割线)
解析:>
第四组例题:>
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));
(分割线-----------------------)
此处的字符数组arr的字符串的形式来初始化,因此在字符串的结尾会默认放一个’\0’,因此,各题的解析为:>
1.sizeof(arr)
2.sizeof(arr+0)
3.sizeof(*arr)
4.sizeof(arr[1])
5.sizeof(&arr)
6.sizeof(&arr + 1)
7.sizeof(&arr[0] + 1)
分析出来的结果为这样子:>
程序运行的结果为:>
第五组例题:>
char arr[] = "abcdef";
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));
(分割线----------------)
1.char arr[] = "abcdef";
strlen(arr)
2.char arr[] = "abcdef";
strlen(arr + 0);
3.char arr[] = "abcdef";
printf("%d\n", strlen(*arr));
4.char arr[] = "abcdef";
printf("%d\n", strlen(arr[1]));
5.char arr[] = "abcdef";
printf("%d\n", strlen(&arr));
6.char arr[] = "abcdef";
printf("%d\n", strlen(&arr+1));
7.char arr[] = "abcdef";
printf("%d\n", strlen(&arr[0]+1));
即结果为:>
第六组例题:>
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));
return 0;
(分割线:>)
1.char* p = "abcdef";
printf("%d\n", sizeof(p));
2.char* p ="abcdef";
printf("%d\n", sizeof(p + 1));
3.char* p ="abcdef";
printf("%d\n", sizeof(*p));
4.char* p ="abcdef";
printf("%d\n", sizeof(p[0]));
5,6
char* p = "abcdef";
printf("%d\n", sizeof(&p));
printf("%d\n", sizeof(&p + 1));
7.char* p = "abcdef";
printf("%d\n", sizeof(&p[0] + 1));
因此,我们解析得到的结果是:>
程序运行得到的结果为:>
第七组例题:>
char* p = "abcdef";
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));
(分割线--------------------)
1.char* p = "abcdef";
printf("%d\n", strlen(p));
2.char* p = "abcdef";
printf("%d\n", strlen(p+1));
3.char* p = "abcdef";
printf("%d\n", strlen(*p));
4.char* p = "abcdef";
printf("%d\n", strlen(p[0]));
5.char* p = "abcdef";
printf("%d\n", strlen(&p));
6.char* p = "abcdef";
printf("%d\n", strlen(&p+1));
7.char* p = "abcdef";
printf("%d\n", strlen(&p[0]+1));
分析出来的结果为:>
程序运行的结果为:>
接下来请接受一波二维数组的拷打:>
int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
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]));
(分割线:>)
1.int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
printf("%d\n",sizeof(a));
因此,第一题:>
2.int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
printf("%d\n",sizeof(a[0][0]));
3.int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
printf("%d\n",sizeof(a[0]));
(看一下第一题的那张图噢!)
(在这里的整个数组指的是第一行)
4.int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
printf("%d\n",sizeof(a[0]+1));
5.int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
printf("%d\n",sizeof(*(a[0]+1)));
6.int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
printf("%d\n",sizeof(a+1));
7.int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
printf("%d\n",sizeof(*(a+1)));
8.int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
printf("%d\n",sizeof(&a[0]+1));
9.int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
printf("%d\n",sizeof(*(&a[0]+1)));
10.int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
printf("%d\n",sizeof(*a));
11.int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
printf("%d\n",sizeof(a[3]));
一些其他的题~~~~
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int *ptr = (int *)(&a + 1);
printf( "%d,%d", *(a + 1), *(ptr - 1));
return 0;
}
|