by Spike » Wed Aug 22, 2012 6:28 pm
huh? a pointer is always 4 bytes (8 bytes on a 64bit machine).
while a 20-element byte array is of course 20 bytes...
a pointer to a 20-element byte array is still just 4 bytes. there's just an extra 20 bytes which is not part of the pointer itself, but part of what it points to.
just because the syntax is the same doesn't mean that the hardware instructions are, which means you cannot mix and match them in different C files, because the hardware representation would conflict between modules / .c files.
If you never take the address of the pointer/array, then the *syntax* is the same and you can freely change the definition between pointer and array - so long as you never use sizeof and the prototype of it matches in every single .c file.
pointer arithmatic works just fine regardless of whether the data is at a constant location (array) or a variable location (pointer). The syntax differences really only come when you take the address of a constant vs variable, while the hardware always cares about the difference. Storing the constant (array) address of the data inside a variable (pointer, even if defined const) is no different from storing the number 5 in a an int, and will allow the hardware to access your fixed-address array data through a variable.
When you have position independant code (more common in linux), the 'constant' addresses are calculated based upon the program counter offset. the maths is along the lines of (&dataaddress - baseaddressofinstruction) + instructionpointer, the first two parts being a constant and the second part being a register of some kind, though on x86 there'll be some additional offsets because you cannot directly reference the instruction pointer register.
Remember - a pointer is an integer variable, accessed through the value stored in that integer. An array is an actual chunk of memory, accessed through the constant address of the first byte.
.