3 ms·
It is true that an array "name" is a pointer to its base address... No, it's not. It is true that an expression of array type, when it is not the subject of e
by caf 8y ago
It is true that an array "name" is a pointer to its base address...
No, it's not. It is true that an expression of array type, when it is not the subject of either the unary-& or sizeof operators, evaluates to a pointer to the array's first element.
sizeof array
gives the size of the whole array, not the size of a pointer.
&array
gives the address of the whole array, not the address of a pointer.
- bcaa7f3a8bbc 8y agoI've programming under the useful but wrong assumption, that > array[x] and *(ptr+x) is completely equivalent, so array and ptr is equivalent. until now. Thanks for the clarification.
- 8xde0wcNwpslOw 8y agoStrictly speaking, the initial part of your assumption is not wrong, but the later conclusion is. array[x] and *(array+x) are indeed equivalent for any identifiers 'array' and 'x' (assuming one of those evaluates as a pointer value, and the other as an integer value; otherwise the code is incorrect). In fact, in this context an actual array is not subject to either unary-& or sizeof operators, so it evaluates to a pointer value, fulfilling the precondition. This is why "array subscription" also directly works with pointers (i.e. "ptr[x]"), and from the equivalence above follows one of the common useless facts that you can swap the identifiers (i.e. "x[array]"). (This comment is probably confusing enough without saying that "(&array)[x]" is valid code too, but isn't the same thing as those before.)