S1E24:指针和二维数组
本帖最后由 BaysideLizard 于 2023-11-14 22:22 编辑#include <stdio.h>
//DNX写于2023年10月26日
int main()
{
int array = {0};
int i,j,k = 0;
for (i = 0;i < 4; i++)
{
for(j = 0; j < 5; j++)
{
array =k++;
}
}
printf("指针与二维数组:\n");
printf("打印一下array:\n");
for (i = 0;i < 4; i++)
{
for(j = 0; j < 5; j++)
{
printf("%d ",array);
}
putchar('\n');
}
printf("array + 1 :%p\n",array + 1);
printf("*(array + 1) :%p\n",*(array + 1));
printf("array :%p\n", array);
printf("array :%d\n", array);
printf("&array :%p\n", &array);
printf("**(array + 1) :%d\n", **(array + 1));
printf("*(*(array + 1) + 3) :%d\n", *(*(array + 1) + 3));
printf("array:%d\n", array);
putchar('\n');putchar('\n');
printf("数组指针与二维数组:\n");
int arrayb = {{0,1,2},{3,4,5}};
int (*p) = arrayb;
printf("打印一下arrayb:\n");
for (i = 0;i < 2; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d ",arrayb);
}
putchar('\n');
}
printf("**(p+1): %d\n",**(p+1));
printf("**(arrayb+1): %d\n",**(arrayb+1));
printf("arrayb: %d\n",arrayb);
printf("*(*(p+1)+2): %d\n",*(*(p+1)+2));
printf("*(*(arrayb+1)+2): %d\n",*(*(arrayb+1)+2));
printf("arrayb: %d\n",arrayb);
return 0;
}
/*
结论:
*(array + i) == array
*(*(array + i) + j) == array
*(*(*(array +i) + j) + k ) == array
......
定义一个数组指针int (*p) = arrayb;
*(p + i) == *(arrayb+i) == arrayb
*(*(p + i) + j) == *(*(array + i) + j) == arrayb
*(*(*(p + i) + j) + k) == *(*(*(array +i) + j) + k ) == arrayb
*/
运行结果:
指针与二维数组:
打印一下array:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
array + 1 :000000000061FDB4
*(array + 1) :000000000061FDB4
array :000000000061FDB4
array :5
&array :000000000061FDB4
**(array + 1) :5
*(*(array + 1) + 3) :8
array:8
数组指针与二维数组:
打印一下arrayb:
0 1 2
3 4 5
**(p+1): 3
**(arrayb+1): 3
arrayb: 3
*(*(p+1)+2): 5
*(*(arrayb+1)+2): 5
arrayb: 5
Process returned 0 (0x0) execution time : 0.052 s
Press any key to continue.
在FishC学C的第九天
加油!
页:
[1]