English 中文(简体)
C 多层面阵列中的要点
原标题:Pointer address in a C multidimensional array

我用多面阵列和点子 around。 我正在研究一个方案,其中勾画一个简单阵列的内容和地址。 我在此宣布:

int zippo[4][2] = { {2,4},
            {6,8},
            {1,3},
            {5,7}   };

我目前的理解是,<代码>zippo是个点,它可以持有另外几个要点的地址。 页: 1 地址为:zippo[0],也可持有点人地址zippo(1)zippo zippo[3]

现在发言如下:

printf("zippo[0] = %p
", zippo[0]);
printf("  *zippo = %p
", *zippo);
printf("   zippo = %p
", zippo);

在我的机器上,产出如下:

zippo[0] = 0x7fff170e2230
  *zippo = 0x7fff170e2230
   zippo = 0x7fff170e2230

我完全理解为什么zippo[0]*zippo。 其价值相同。 这两条都重新贴上了两点,并储存了第二版(违约)的地址,或zippo[0][0]。 但是,有<代码>zippo。 还分享同样的记忆地址? 是否应有t <代码>zippo? Whaaat?

最佳回答

当你宣布一个多层面阵列时,汇编者将其视作一个单一的方面阵列。 多层面阵列只是使我们的生活更加容易的抽象概念。 你有误解: 这个阵列指4个阵列,它始终只是一个单一的传承区。

在你的情况下:

int zippo[4][2]

确实与这样做一样。

int zippo[8]

2D地址所需的数学由编辑处理。

详情见C++。

这与以下做法非常不同:

int** zippo

int* zippo[4]

在这种情况下,你重新设置了4个点,可以分配给其他阵列。

问题回答

多数情况下出现阵列时,其类型从“T号”的“非要素阵列”改为“T”,其价值指阵列中的第一个要素。 本条规则的例外是,阵列的表述是sizeof或(&)操作者的操作方式,或者说阵列实际上被用作声明的初始动力。

因此,<代码>zippo <代码>int [4]/(4-element range of 2-element ranges of int)至int (*) (*) (pointer to 2-element range of int)。 同样,<代码>Zppo[0]>的类型是int / ,其中隐含地改为int *

鉴于声明int zippo[4]/code>,以下表格显示了涉及电离层的各种阵列表述类型和任何默示转换:

Expression    Type            Implicitly converted to  Equivalent expression
----------    ----            -----------------------  ---------------------
zippo         int [4][2]      int (*)[2]               
&zippo        int (*)[4][2]       
*zippo        int [2]         int *                    zippo[0]
zippo[i]      int [2]         int *
&zippo[i]     int (*)[2]                               
*zippo[i]     int                                      zippo[i][0]
zippo[i][j]   int
&zippo[i][j]  int *
*zippo[i][j]  invalid

注:zippo ,&zippo,*zippo[0],&zippo[0];&zippo[0][0]。 所有这些都具有同样的价值;它们都指阵列的基础(阵列的地址与阵列第一个要素的地址相同)。 但各种表述的类型各不相同。

<代码>zippo不是点。 它包含一系列的数值:zippozippo[i],0..4 可在某些情况下(特别是在价值方面)向点人“衰败”。 缩略语 在这种情况下,sizeof将报告阵列的规模,而不是点名大小。

阵列的名称,in Value context, decays to a pointer to its first content. 因此,在价值方面,zippo&zippo[0],从而具有“在>的阵列上点”;*zippo,在价值方面与&zippo[0][0]相同,即“点到int。 它们的价值相同,但不同类型。

我建议阅读,回答你的第二个问题。 点数与“价值”相同,但指出的空间数量不同。 印刷<代码>zippo+1和*zippo+1,以便更清楚地看到:

#include <stdio.h>

int main(void)
{
    int zippo[4][2] = { {2,4}, {6,8}, {1,3}, {5,7} };
    printf("%lu
", (unsigned long) (sizeof zippo));
    printf("%p
", (void *)(zippo+1));
    printf("%p
", (void *)(*zippo+1));
    return 0;
}

我的行文是:

32
0xbffede7c
0xbffede78

电话:sizeof(int) on my organ is 4, and that the second and the third pointers are not equal in Value (as expected).

此外,%p>specifier needs ave * in *printf() function,因此,您应将点人排到voidf(printf(>>>>),成为单体功能,因此汇编者可以在此自动转换。

。 当我向点人说一阵“dec”时,我指的是,在价值方面阵列的名称相当于点子。 因此,如果我有<代码>T pt[100]; for some category T,则名称pt。 T* in Value context. <sizeof and unary &mente, the name pt 确实如此。 但你可以做<代码>。 T *p = pt;——这一条完全有效,因为在这方面,<代码>pt<>code>为。 T *

请注意,这种“衰败”只发生一次。 因此,我要说:

int zippo[4][2] = { {2,4}, {6,8}, {1,3}, {5,7} };

然后,zippo, in Value context decays to a pointer of category: pointer to range /code>int。 法典:

int (*p1)[2] = zippo;

有效,

int **p2 = zippo;

将引发“不相容的点派”警告。

以上定义的“编码”

int (*p0)[4][2] = &zippo;
int (*p1)[2] = zippo;
int *p2 = zippo[0];

are all valid. They should print the same value when printed using printf("%p ", (void *)name);, but the pointers are different in that they point to the whole matrix, a row, and a single integer respectively.

这里的重要内容是<编码>int zippy[4]/code>。 物体类型与<代码>int **zippo不同。

就像int zippi[5],zippy,是记忆的一部分。 But,汇编者知道,你希望从<代码>zippy开始,用两个层面的综合星号处理8个记忆地点,但希望从zippi开始处理5个记忆地点,并有一个层面的综合星号。

<代码>zippo完全不同。 It 拥有一个足以包含两个点的记忆组的地址,如果您在一些阵列上them点,你可以将其与两个阵列的接触辛提。

如果Reed作了很好的解释,我将再补充几个要点,使之更为简单,当我们提到<条码>[0] or zippo[0][0]时,我们仍在提及阵列代码的相同基地址。 阵列的原因始终是记忆的毗连部分,多层面阵列是连续设置的多个单一层面阵列。

When you have to increment by each row, you need a pointer int *p = &zippo[0][0], and doing p++ increments the pointer by every row. In your example id its a 4 X 2 array, on doing p++ its, pointer currently points to second set of 4 elements.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签