9

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

Community
  • 1
  • 1

4 Answers4

15

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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 5
    x[2] is the same as *(&x[0] + 2); did you mean x + 2 is the same as &x[0] + 2? There's a problem defining subscripting in terms of &x[0] (which is also subscripting). – Fred Nurk Nov 19 '10 at 09:44
  • 1
    Another such context is when the array is bound to a reference to array. Cheers, – Cheers and hth. - Alf Nov 19 '10 at 10:01
  • 2
    @Fred: Ugh, that was really bad; thanks for pointing it out. I actually meant `(&x[0])[2]`, since I was attempting to show equivalence between `x` and `&x[0]`. Your example would work too, though. – James McNellis Nov 19 '10 at 17:59
  • @Alf: Thanks for reminding me about the reference-to-array issue. There's also the case when a string literal (which is an array) is used as an initializer. Have we missed any other cases or is that all of them? – James McNellis Nov 19 '10 at 18:00
2
int a[6];
int *b = a;

Pointer b points to the a[0], i.e. contains the address of the element a[0].

BЈовић
  • 62,405
  • 41
  • 173
  • 273
1

This means, that you can have the following situation:

int arr[100];
arr[ 0 ] = arr[ 1 ] = 666;
// ..

You can use arr as pointer to int, which points to the first element of the array, for example:

*arr = 123;

and then the array will be: arr = { 123, 666, ... }

Also, you could pass the array to a function, that takes int*:

void f( int* a ) { /* ... */ }

and call it:

f( arr );

It's absolutely the same as calling it like this:

f( &arr[ 0 ] );

That is what The result is a pointer to the first element of the array. means.


Another way, you could use the address of the first element is:

*( &arr[ 0 ] + 1 ) = 222;

this will make the second element in the array with value 222; It's the same as

arr[1] = 222;

and

*( arr + 1 ) = 222;
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
0

One example of this is that any array variable will automatically degenerate into a pointer to it's first element when passed to a function which takes a pointer of the array's type.

Take a look at this section from the C-Faq on Arrays and Pointers. This is equally applicable in C++.

void foo(int *a) {
    a[0] = 1;
}

int main(void) {
    int b[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    foo(b);

    printf("b[0] == %d\n", b[0]);
}
Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179