Roald de Vries wrote:
<div class="moz-text-flowed" style="font-family: -moz-fixed">On Aug
15, 2010, at 2:16 PM, geremy condra wrote:
On Sun, Aug 15, 2010 at 4:55 AM, Roald de Vries <downa...@gmail.com>
wrote:
On Aug 15, 2010, at 1:00 PM, Lawrence D'Oliveiro wrote:
It would be if pointers and arrays were the same thing in C. Only
they’re
not, quite. Which somewhat defeats the point of trying to make them
look
the
same, don’t you think?
How are they not the same?
The code snippet (in C/C++) below is valid, so arrays are just
pointers. The
only difference is that the notation x[4] reserves space for 4
(consecutive)
ints, and the notation *y doesn't.
int x[4];
int *y =x;
Moreover, the following is valid (though unsafe) C/C++:
int *x;
int y = x[4];
Just to demonstrate that they are different, the following code
compiles cleanly:
int main() {
int *pointer;
pointer++;
return 0;
}
While this does not:
int main() {
int array[0];
array++;
return 0;
}
Interesting! Thanks for the lesson ;-).
Cheers, Roald
That particular example doesn't really matter; it just shows that array
is a *const* pointer.
One that does matter, sometimes drastically, is sizeof(array) vs.
sizeof(pointer).
One interesting other effect of the compiler (nearly always) treating
pointers and arrays the same is expressions like:
int hexdigit = ...something...
char ch = "0123456789abcdef"[hexdigit];
char ch2 = hexdigit["0123456789abcdef";
both are valid assignments, and equivalent to
char ch3 = hexdigit + &("0123456789abcdef");
--
http://mail.python.org/mailman/listinfo/python-list