In C, if we have an array like a[10], then a and &a have the same pointer value (but not the same type). I want to know why was C designed like this?
Was this to save the additional space required for storing &a? … This makes sense when you think of the fact that a can never point to any other location, so storing &a is meaningless.
This isn’t a fact, though. If
ais an array,adoesn’t point anywhere becauseais not a pointer. Givenint a[42];,anames an array of 42intobjects; it is not a pointer to an array of 42intobjects (that would beint (*a)[42];).&xgives you the address of the objectx; ifxis an array type variable, then&xgives you the address of the array; if nothing else, this is consistent with the behavior of&for any other object.A better question would be “why does an array (like
a) decay to a pointer to its initial element in most cases when it is used?” While I don’t know with certainty why the language was designed this way, it does make the specification of many things much simpler, notably, arithmetic with an array is effectively the same as arithmetic with a pointer.