Reputation: 33
How come double* array can be used as an array? I always see the asterisk as a pointer, how come it could be used as an array? Will anyone provide an example how it could be used as a double[], and when it is only a pointer.
Upvotes: 0
Views: 210
Reputation: 283634
I assume that "used as an array" means the subscript operator []
?
The reason is that in C and C++, the subscript operator actually performs pointer addition. It doesn't work on arrays at all, it causes the array name to decay to a pointer, and then pointer arithmetic occurs.
For all built-in types,
x[y]
is defined as
*(x + y)
It doesn't matter which of x
and y
is the pointer (or array decaying to pointer) and which is the offset.
Upvotes: 2