指针运算和数组:什么是真正合法的吗? [英] Pointer arithmetic and arrays: what's really legal?

查看:146
本文介绍了指针运算和数组:什么是真正合法的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的语句:

int    *pFarr, *pVarr;

int    farr[3] = {11,22,33};
int    varr[3] = {7,8,9};

pFarr = &(farr[0]);
pVarr = varr;

在此阶段,两个指针都在每个相应的阵列地址的开始指向。对于* pFarr,我们是presently看着11和* pVarr,7。

At this stage, both pointers are pointing at the start of each respective array address. For *pFarr, we are presently looking at 11 and for *pVarr, 7.

同样的,如果我通过*法尔要求每个阵列的内容,* varr,我也得到11和7。

Equally, if I request the contents of each array through *farr and *varr, i also get 11 and 7.

到目前为止好。

现在,让我们试着 pFarr ++ pVarr ++ 。大。我们现在正在寻找22和8,符合市场预期。

Now, let's try pFarr++ and pVarr++. Great. We're now looking at 22 and 8, as expected.

但现在...

试图拉升法尔+​​+ varr ++ ...我们得到错误类型参数递增的

Trying to move up farr++ and varr++ ... and we get "wrong type of argument to increment".

现在,我识别数组的指针和常规指针之间的差,但由于它们的行为是相似的,为什么这种限制

Now, I recognize the difference between an array pointer and a regular pointer, but since their behaviour is similar, why this limitation?

这是进一步迷惑我,当我还认为,在同一个程序,我可以调用下面的函数在一个表面上是正确的方式,而在另一个不正确的方法,我也得到了相同的行为,但在对比中发生了什么code上面贴!?

This is further confusing to me when I also consider that in the same program I can call the following function in an ostensibly correct way and in another incorrect way, and I get the same behaviour, though in contrast to what happened in the code posted above!?

working_on_pointers ( pFarr, farr );  // calling with expected parameters
working_on_pointers ( farr, pFarr );  // calling with inverted parameters 

void working_on_pointers ( int *pExpect, int aExpect[] ) {

    printf("%i", *pExpect);  // displays the contents of pExpect ok
    printf("%i", *aExpect);  // displays the contents of aExpect ok

    pExpect++;               // no warnings or errors
    aExpect++;               // no warnings or errors

    printf("%i", *pExpect);  // displays the next element or an overflow element (with no errors)
    printf("%i", *aExpect);  // displays the next element or an overflow element (with no errors)

}

有人能帮助我理解为什么指针数组和指针,在某些情况下类似的方式行事,但在其他方面有什么不同?

Could someone help me to understand why array pointers and pointers behave in similar ways in some contexts, but different in others?

这么多的感谢。

编辑:的像我这样的菜鸟可以从这个资源进一步受益: http://www.panix.com/~elflord/cpp/gotchas/index.shtml

Noobs like myself could further benefit from this resource: http://www.panix.com/~elflord/cpp/gotchas/index.shtml

推荐答案

所不同的是,因为法尔+​​+ 来有什么影响,什么地方,编译器会需要存储法尔将评估到阵列的第二个元素的地址。但对这些信息的地方。编译器只对 3 整数分配到位。

The difference is because for farr++ to have any effect, somewhere the compiler would need to store that farr will evaluate to the address of the second element of the array. But there is no place for that information. The compiler only allocates place for 3 integers.

现在,当你宣布一个的功能参数的是一个数组,函数的参数不会是一个数组。函数参数将是一个指针。有在C没有数组参数,所以下面的两个声明是等价的。

Now when you declare that a function parameter is an array, the function parameter won't be an array. The function parameter will be a pointer. There are no array parameters in C. So the following two declarations are equivalent

void f(int *a);
void f(int a[]);

它甚至不不管你放什么号码括号之间 - 因为参数真的会是一个指针,大小只是忽略。

It doesn't even matter what number you put between the brackets - since the parameter really will be a pointer, the "size" is just ignored.

这是功能相同的 - 下面两个是等价的,有一个函数指针作为参数:

This is the same for functions - the following two are equivalent and have a function pointer as parameter:

void f(void (*p)());
void f(void p()); 

虽然你可以调用这两个函数指针和功能(所以他们使用了类似的),你也不能写一个函数,因为它不是一个指针 - 它仅仅是转换的指针:

f = NULL; // error!

大部分不能修改阵列以同样的方式。

Much the same way you can't modify an array.

这篇关于指针运算和数组:什么是真正合法的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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