Pointer is never really casted "up" or "down" in this case. Compiler simply produces code that reads or writes specified number of bytes (depending on type, int=4 bytes, short=2 bytes, etc) from/to memory. It's up to you to guarantee correct behavior of program when reading/writing memory. Memory is just an array of bytes, it's up to you how to "view" it - which 4 bytes in which places is actually an unsigned int variable.
This would print
Be careful with this assumption, it is not universally true. Yes, on Intel CPU it will do that, but on other architectures it could print 0. Some architectures are little endian, some are big.
Little endian means that bytes in integer are stored to memory in a way where least signficent are stored first. Big-endian is other way around.
On little-endian CPU like Intel your integer is stored in following way:
| +-----+-----+-----+-----+
| 255 | 0 | 0 | 0 |
+-----+-----+-----+-----+
|
But on big-endian (like PowerPC, or some MIPS and ARM) it will be stored like this:
| +-----+-----+-----+-----+
| 0 | 0 | 0 | 255 |
+-----+-----+-----+-----+
|
Does this happen because the pointer reads more memory than I actually set?
Exactly.
If you would look in memory window with debugger you would see something like this:
| +-----+-----+-----+-----+
... | 255 | ??? | ??? | ??? | ...
+-----+-----+-----+-----+
^
|
+-- this is where "integer" variable is stored (it's address)
|
Where ??? are unknown values. They could be the same every time you execute, they could be different. It depends on code.
This kind of code behaviour is called "undefined". What that typically means is that compiler is free to do whatever it wants. In this case it simply reads whatever is in memory and gives back the value. But it is free to simply terminate program, or return from function or remove code and not execute it. Be careful!
Would then the value pointerInt points to be consistent every time the program is executed?
It depends on code. Usually it is not consistent.
| unsigned char integer = 255;
unsigned int integer = 3000;
|
I assume second integer is really integer2 (with value 3000) ?
I increase the range size of pointerInt. So pointerInt points to the same location as pointerChar did before. But I's range is bigger because its of type int. So it should now read "char integer" and 3/4rth of "char integer2".
Yes and no. It depends on compiler. Compiler is free to put variables on stack in whatever order. Or not put at all. Or put some other internal temporary data between your variables. If it can figure out what are you doing with variables in your function (like all your examples here), it can simply print out constant values without reading and writing to stack. Compilers can do this kind of analysis pretty trivially. You should never rely on stack layout of local variables in your program.
For example, look here at disassembly:
https://godbolt.org/g/HSYKcF
You can see that compiler simply calls "print" function two times with constant 255 as argument. No reading or writing to stack. It figured out what your function is doing at compile time.