standard conversions: Array-to-pointer conversion

后端 未结 4 1398
予麋鹿
予麋鹿 2020-11-29 10:33

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 o

4条回答
  •  粉色の甜心
    2020-11-29 11:29

    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;
    

提交回复
热议问题