3 ms·
Will I be correct if I say that in the end, the difference is that arrays are immutable (in the address that they point to) and pointers are not?
by golergka 12y ago
Will I be correct if I say that in the end, the difference is that arrays are immutable (in the address that they point to) and pointers are not?
- krylon 12y agoIn part. Pointers can be made constant in two ways, a) constant in that they cannot be used to change the memory they point to, and b) in that one cannot change what memory they point to. char const * ptr_a; /* You cannot write to the memory pointed to by ptr */ char * const ptr_b; /* You cannot change what ptr_b points to */ char const * const ptr_ab; /* You neither change what ptr_ab points to nor the memory it points to - at least not through this pointer */
- noselasd 12y agoWell, there's some other important differences sizeof somearray vs sizeof somepointer are naturally different. &somearray vs &somepointer are very different types. Which also means that somearray + 10 vs somepointer + 10 yields very different results.
- Peaker 12y agoNo. An array has size (N * sizeof(element)). Arrays are immutable in their address in the same sense that any lvalue is immutable in its address. int x; int y[2]; The two declarations above are similar, one happens to be an (int). The other an (int[2]). The difference is that when you take the rvalue of an int, say x, you get an rvalue int. When you take the rvalue of an int[2], you get a degraded (int *) rvalue that points to its first element. Until you convert the array object to an rvalue, it is no more related to an (immutable) pointer than the above int. Both have an immutable address, like all lvalues do.