Array is not pointerPublished by siavoshkcLast 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 writeint 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 writingint 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 exampleint *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.

评论