Search code examples
carraysimplicit-conversionrvalueunary-operator

I can't figure out the point about the array name


I've recently posted a few questions about array names here.

Thanks to a lot of people, now i know that the array name is not a pointer, and also know that the array name is an non-modifiable l-value.

I'm have one more question about array.

  1. I was told that the array name is converted into the address of the first element of the array.

So I understand that the array name is returned like this.

int arr[10];
arr == &arr[0];

But if it is right, isn't it an l-value because the returned value is an address value?

Am I still misunderstanding?


Solution

  • I was told that the array name is converted into the address of the first element of the array.

    This applies as part of the evaluation of expressions in which the array's identifier appears, and there is a handful of exceptions.

    So I understand that the array name is returned like this.

    int arr[10];
    arr == &arr[0];
    

    The automatic conversion to a pointer -- when it occurs -- is analogous to your expression &arr[0] (which in fact depends on that conversion in the first place), but it is incorrect to think of it as such a conversion being performed immediately and permanently when the array is declared. An array's identifier identifies the array, not only its address or its first element. In particular, the exceptions to the automatic conversion to a pointer factor in here, especially the fact that if arr is the identifier of an array, then sizeof(arr) evaluates to the size of that array, not the size of a pointer.

    But if it is right, isn't it an l-value because the returned value is an address value?

    It is not right. The reason that an array's identifier is an lvalue is that it designates the array.