Result of sizeof for C++ arrays and pointers

What is the intuition behind why sizeof(z) does not return 8?

z is not a pointer. Hence sizeof(z) is not anything, but 5 bytes. In case of sizeof, the array doesn't decay to pointer. Refer: What is array decaying?


There are several implicit conversions in C++ like array to pointer, enum to integer, double to float, derived to base, any pointer to void* and so on. Which may lead us to think if their sizes are same or what?
Hence, a litmus test for self understanding is to create a pointer reference & try to assign the other type. It results in error for non matching types. e.g.

int *x = new int[5], *&px = x; // OK
int z[5], *&pz = z; // error: can't initialize

You've defined x as a pointer to char, so sizeof(x) yields the size of a pointer to char. On a current implementation, that'll typically be either 32 bits or 64 bits. A char is typically 8 bits, so you can expect sizeof(char *) to yield 4 or 8 on most current compilers.

You've defined z as an array of 5 char, so sizeof(z) yields the size of an array of 5 char. Since the elements of an array are contiguous, and sizeof(char) is guaranteed to be 1, the obvious value for that would be 5.

If (for example) you put an array of 5 char into a struct, and that's followed by (say) an int, there's a very good chance the compiler will insert some padding between those two elements.