| siavoshkc (21) | |||||||||||||||
|
With many thanks for these 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 a sequence of simple variables in memory. When we write
C/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
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
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 (a memory address to an array not an array member). Here is another example to make the article more understanding. I want to write a program that gets an integer from user, adds 4 to it and then prints out the result. Once I write it using an integer pointer and once with an integer variable. With integer it will be:
And using a pointer it will be:
Who thinks these programs are exactly the same? Lets take a look at their assembly. For the first code with an integer it is:
And for the code with pointer it is:
19 lines Vs 32 lines. Therefore, you see, an integer is different to a "pointer to integer". An integer is a place of memory where an integer number is kept but an integer pointer (pointer to integer) is a place of memory where an address is saved. Compiler knows that is an address of a place of memory where an integer is held. I do not explain the assembly code since this article is for beginners and I want to keep it short. As I said array is a sequence of variables in memory. In the example above, I concluded that a pointer is not an integer variable, so it is clear that it cannot be a sequence of them too. Please feel free to send comments and your idea about the article. | |||||||||||||||
|
|
|||||||||||||||
| Graham (15) | |||||||||
|
You have a point, but you have one flaw: ARRAYS ARE CONSTANT POINTERS. The code
is the same as
And the subscript operator ([]) is really a dereference operator with a shift, so
is really
Because of pointer arithmetic, adding X to an address of type T is the same as shifting that address by X*sizeof(T). The handling of arrays as pointers is very crucial to an understanding of C. For example, you can pass an array anywhere a pointer is expected, and vice versa. A function can handle arrays, but define the type it recieves as a pointer. And since arrays are pointers (constant pointers), they are passed call by reference, meaning the array passed will be modified immediately in the calling function. You probably have some sort of optimizing compiler that causes the phenomenon you are describing. | |||||||||
|
|
|||||||||