正文

Array is not pointer2006-05-29 08:47:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/bclz/15140.html

分享到:

Array is not pointer
Published by siavoshkc
Last update on Feb 20, 2006 at 5:48amWith many thanks for your useful tutorials, I felt it's necessary to send this text about pointers and arrays. Unfortunately pulling out something wrong that is put in humans head is a bit difficult. So understanding the things correct and precise is very important to avoid further misconceptions.
An array is not equal to a pointer. It is more like a simple variable in definition.
When we write
int array[3];array[2]=666;
C compiler doesn't see array[0] as an address to an integer value, it takes it directly as a value, exactly as same as writing
int var;var=66;
That's obvious that "var" is not a pointer exactly as array[2] is not.
But if we use a pointer instead of an array, the face of the code is the same but compiler generates different assembly code. For example
int *ptr = new int[3];ptr[2] = 66;
Is similar to the first code but not with the same meaning to the compiler. In the first code (second line), compiler generates code that will do the following:

1) Go two places next of the array[0] and make it equal to 666.
But in code with pointer it is:
1) Fetch the value (address) of the ptr[0].
2) Add two to it.
3) Make the value pointed by it to 66.
Actually the value of "array", "&array" and "&array[0]" is equal. But the type of "&array" is different.

阅读(403) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册