Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

c++ - standard conversions: Array-to-pointer conversion

This is the point from ISO :Standard Conversions:Array-to-pointer conversion: $4.2.1

An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to an rvalue of type “pointer to T.” The result is a pointer to the first element of the array.

Can any one explain this, if possible with an example program.

I seen these links already, but i am unable to understand:

Array and Rvalue

I think I may have come up with an example of rvalue of array type

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In both C and C++, an array can be used as if it were a pointer to its first element. Effectively, given an array named x, you can replace most uses of &x[0] with just x.

This is how subscripting is able to be used with array objects:

int x[5];
x[2];     // this is the same as (&x[0])[2]

This is also how an array can be passed to a function that has a parameter of pointer type:

void f(int* p);

int x[5];
f(x);     // this is the same as f(&x[0])

There are several contexts in which the array-to-pointer conversion does not take place. Examples include when an array is the operand of sizeof or the unary-& (the address-of operator), when a string literal is used to initialize an array, and when an array is bound to a reference to an array.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...