the difference between array identifier and address of array identifier

前端 未结 4 1070
自闭症患者
自闭症患者 2021-01-14 07:23

Following program would state my doubt clearly I think,so I posted the program:

 #include 
int main() {

        int a[]={1,2,3,4,5};
                 


        
4条回答
  •  滥情空心
    2021-01-14 07:43

    When it is not the subject of the sizeof or unary & operators, an array evaluates to a (non-lvalue) pointer to its first element.

    So &a is the address of the array a, and a evaluates to the address of the first element in the array, a[0].

    That the address of the array and the address of the first element in the array are the same is not surprising (that is, they point to the same location even though they have different types); the same is true of structs as well. Given:

    struct {
        int x;
        int y;
    } s;
    

    Then &s and &s.x point to the same location (but have different types). If converted to void * they will compare equal. This is exactly analogous with &a and &a[0] (and consequently just a).

提交回复
热议问题