简体   繁体   English

关于C语言中指针功能的混乱

[英]In chaos regarding function of pointers in C Language

I wrote a program to test pointer actions but I didn't get the logic behind output. 我编写了一个程序来测试指针操作,但我没有得到输出背后的逻辑。

As if I use arr+1 then instead of 2nd location I get 7th and similar is the case with *arr+1 and I feel weird for * expression is showing address not value. 好像我使用arr+1而不是第二个位置我得到第7个,类似于*arr+1 ,我觉得奇怪的是*表达式显示的地址不是值。

CAN ANYONE EXPLAIN OUTPUT, PLEASE ! 可以任何人解释输出,请!

Program is : 计划是:

#include<stdio.h>
int main()
{
    static int arr[2][3][2] = {1,2,3,4,5,6,7,8,9,10,11,12};
        int i,j,k;
        printf("%d\t",arr);
        printf("%d\t",*arr);
        printf("%d\n",**arr);
        printf("%d\n",***arr);
        printf("%d\t",arr+1);
        printf("%d\t",*arr+1);
        printf("%d\n",**arr+1);
        printf("%d\n",***arr+1);
        for(i=0;i<2;i++)
        {
            for(j=0;j<3;j++)
            {
            for(k=0;k<2;k++)
                printf("%d      %u \n",*(*(*(arr+i)+j)+k),&arr[i][j][k]);
                printf("\n");
            }
        printf("\n");
        }
return 0;
}

And OUTPUT is: 而OUTPUT是:

4202512 4202512 4202512
1
4202536 4202520 4202516
2
1      4202512
2      4202516

3      4202520
4      4202524

5      4202528
6      4202532

7      4202536
8      4202540

9      4202544
10      4202548

11      4202552
12      4202556

you declare a int [2][3][2] array, which means that: 你声明一个int [2][3][2]数组,这意味着:

  • the type of the expression arr is an int [2][3][2] array, so you can only get a pointer from that type. 表达式arr的类型是int [2][3][2]数组,因此您只能从该类型获取指针。
  • the type of the expression arr[0] , or *arr is an int [3][2] array, so you can only get a pointer from that type. 表达式arr[0]的类型,或*arr是一个int [3][2]数组,因此您只能从该类型获取指针。
  • the type of the expression arr[0][0] , or **arr is an int [2] array, so you can only get a pointer from that type. 表达式arr[0][0]**arrint [2]数组,因此您只能从该类型获取指针。
  • the type of the expression arr[0][0][0] , or ***arr is an int , so you can only get a int value from that type. 表达式arr[0][0][0]***arrint ,因此您只能从该类型获取int值。

variations of this process will give similar outcome. 这个过程的变化将给出类似的结果。

you always get a same base address because multidimensional arrays are flat in memory and dimension sizes are used for partitioning that flat memory. 你总是得到一个相同的基地址,因为多维数组在内存中是平的,尺寸大小用于分区那个平坦的内存。

int [2][2] is like: int [2][2]如下:

0, 1
2, 3

Where the numbers resemble the order of elements in memory, and the bidimensional display resemble the array's line/column access. 数字类似于内存中元素的顺序,而二维显示类似于数组的行/列访问。

Your first 3 prints are showing you the same address but have different meanings (and types)- 您的前三个打印件显示相同的地址,但具有不同的含义(和类型) -

  1. arr is the address of your entire 3d array, of type int*** (you cast the pointer to int, not a good practice since on some systems it may be bigger) arr是整个3d数组的地址,类型为int*** (你将指针强制转换为int,这不是一个好习惯,因为在某些系统上它可能更大)
  2. *arr is the address of the "flattened" 2d array of type int** , effectively located at arr[0] *arrint**类型的“flattened”二维数组的地址,有效地位于arr[0]
  3. **arr is the address of the twice "flattened" 1d array of type int* , effectively located at arr[0][0] **arrint*类型的两次“flattened”1d数组的地址,实际上位于arr[0][0]
  4. Only ***arr finally brings you to an actual int value of arr[0][0][0] 只有***arr最终会带你到arr[0][0][0]的实际int值

Now you start pointer arithmetics, that's determined by the type of the pointer as mentioned abode and by the size of each dimension (which is known to the compiler). 现在,您启动指针算术,这取决于所提到的指针类型和每个维度的大小(编译器已知)。 So: 所以:

  1. arr+1 gives you the address of arr[1] , which is removed from arr[0] by the length of 2*3 int elements (6*4=24 here) arr+1给出了arr[1]的地址,它从arr[0]删除了2 * 3 int元素的长度(这里6 * 4 = 24)
  2. *arr+1 gives you the address arr[0][1] , which is removed from arr[0][0] by the length of 2 int elements (2*4=8 here) *arr+1给你的地址是arr[0][1] ,它从arr[0][0]删除了2个int元素的长度(这里2 * 4 = 8)
  3. **arr+1 gives you the address of arr[0][0][1] , which is removed from arr[0][0][0] by the length of one int element (4 here) **arr+1给出了arr[0][0][1] ,它从arr[0][0][0]移除一个int元素的长度(这里为4)
  4. ***arr+1 gives you arr[0][0][1] , which is simply 2 ***arr+1给你arr[0][0][1] ,这只是2

See also this answer - How are 3D arrays stored in C? 另请参阅此答案 - 3D数组如何存储在C中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM