質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top