为什么指针占用6个字节? [英] Why pointer is taking 6 bytes?

查看:296
本文介绍了为什么指针占用6个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计为什么在我的2d数组中两个指针之间占用6个字节的空间?



Guys why is it taking 6 bytes of space between two pointers in my 2d array?

#include<stdio.h>

int main()
{
    int a[2][3]={{0,1,2},{3,4,5}};

    int (*ptr)[3]=a;
    //ptr= a;
    printf("%p\n%p\n%p\n",ptr,((ptr+0)+1),((ptr+0)+2));
        printf("%d\n%d\n%d\n",*(*(ptr)+0),*(*(ptr+0)+1),*(*(ptr+0)+2));
    return 0;

}



输出:

0xbfb2ab98

0xbfb2aba4

0xbfb2abb0

0

1

2


output:
0xbfb2ab98
0xbfb2aba4
0xbfb2abb0
0
1
2

推荐答案

它不是' t。

这些是十六进制数字所以:

It isn't.
Those are Hexadecimal numbers so:
0xbfb2aba4 - 0xbfb2ab98 != 6

相反:

Instead:

0xbfb2aba4 - 0xbfb2ab98 == 12 (decimal) or 0x0c (Hexadecimal)


您的 ptr 定义一个指向三个 int 的数组的指针(即矩阵的一行)。

如果执行指针运算,编译器会为您进行大小计算。如果将1添加到当前行地址,则会获得下一行的地址。

您似乎在 int <的机器上工作/ code>的大小为4个字节,因此该行的大小为3 * 4 = 12个字节。这导致前一行相距12个字节的下一行。



指针算法非常混淆。为什么你这样做?改为使用指数!如果您需要某个元素的地址,请使用address-of运算符,例如&(a [1] [2]); 等。

干杯

Andi
Your ptr defines a pointer to an array of three ints (i.e. one row of your matrix).
If you do pointer arithmetic, the compiler does for you the size calculations. If you add 1 to the current row address, you get the address of the next row.
You seem to work on a machine where int has a size of 4 bytes, so the row has size of 3*4 = 12 bytes. This leads to a next row at 12 bytes apart of the former row.

You pointer arithmetic is very much obfuscating. Why the heck do you do this? Work with indices instead! And if you need the address of some element, use the address-of operator, e.g. &(a[1][2]);, etc.
Cheers
Andi


答案1和2都是正确的。但是,您的上一条评论显示您的代码中仍然存在某种意外。如果ptr指向int [3]数组,为什么第二个printf显示连续值0,1,2呢?



答案部分包含在你的评论:

然后((ptr + 0)+0)是ptr [0] [0],((ptr + 0)+1)是ptr [0] [1]和((ptr + 0)+2)是ptr [0] [2]对吧?



不,这是不正确的。 ((ptr + 0)+1)与ptr + 1相同,并且再次与& ptr [1]相同,并且再次与& ptr [1] [0]相同。括号在算术表达式中一如既往地工作。这就是你在第一个printf中看到增量为12的原因。


在你的第二个printf中,你写了像*(*(ptr + 0)+1这样的表达式)。请注意,取消引用运算符(*)绑定比添加更强,因此这意味着:

Answers 1 and 2 are both correct. But nevertheless, your last comment shows that there is still some kind of surprise in your code. If ptr points to int[3] arrays, why does the second printf show the consecutive values of 0, 1, 2 then?

The answer is partly contained in your comment:
"Then ((ptr+0)+0) is ptr[0][0], ((ptr+0)+1) is ptr[0][1] and ((ptr+0)+2) is ptr[0][2] right?"

No, that is not correct. "((ptr+0)+1)" is the same as "ptr+1" and that again is the same as &ptr[1] and that again is the same as &ptr[1][0]. The parentheses work just as always in arithmetic expressions. That is the reason you see an increment of 12 in the first printf.

In your second printf, you wrote expressions like *(*(ptr+0)+1). Notice that the dereference operator (*) binds stronger than the addition, so that means:
*(   (*(ptr+0))  + 1  )



再次是


which again is

*( (*ptr) + 1 )



* ptr提供int [3]。您不能将1添加到整数数组,因此编译器会将其转换为int *。添加1将获得下一个连续的int单元格。这就是你看到0,1,2的原因。
或者,换句话说,(*(ptr + 0)+1)确实对应于& ptr [0] [1],虽然((ptr + 0)+1)没有。



现在知道了吗?


The *ptr delivers a int[3]. You can't add 1 to an integer array, so the compiler converts it to an int*. Adding 1 gets you the next consecutive int cell. And that is why you are seeing 0, 1, 2.
Or, in other words, (*(ptr+0)+1) does correspond to &ptr[0][1], while ((ptr+0)+1) does not.

Got it now?


这篇关于为什么指针占用6个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆