Let's say I have a char pointer:
char * cp;
int val2 = 2;
cp = &val2;
*cp = 1234;
What will happen since the value 1234 is too large to fit in 1 byte? Will it just overflow and cause incorrect values to be stored, or will it somehow store the correct value across several bytes?
In *cp = 1234;
, *cp
refers to just one byte, the first (lowest-addressed) byte of val2
.
In an assignment, the value of the right-hand side is converted to the type of the left side. So, 1234 will be converted to char
.
There are two complications here. One, in most C implementations, 1234 is too big to fit into a char
. (The C standard allows a char
to be larger than eight bits, but this is rare in modern computers.) Two, char
may be signed or unsigned.
If char
is unsigned, then the conversion is well defined: 1234 is reduced modulo one more than the largest value of char
(usually 255, so the reduction is modulo 256). In the usual case, 1234 will be reduced to 210, and this will be stored in the byte referred to by *cp
.
If char
is signed and 1234 does not fit in a char
, then the conversion is implementation-defined or an implementation-defined signal is raised. In many modern C implementations, the result of the conversion will be -46.
The byte that cp
points to after cp = &val2;
is the lowest-addressed byte of val2
. It is not necessarily the least significant byte of val2
. Some C implementations store integers with the least significant byte lowest in memory (little endian), and some C implementations store integers with the most significant byte lowest in memory (big endian). (There are even systems that do not store the bytes in order; they may be mixed up in memory.)
Most modern C implementations store integers in two’s complement form without padding, so, once you know whether the C implementation is big-endian or little-endian, you would know how changing one byte would affect the value of the int
. However, the C standard still permits sign-and-magnitude or ones’ complement. It also permits padding bits in signed integers.
Finally, note that char
is a special type which permits accessing parts of other objects in this way. If, for example, you used short
instead of char
, as in short *sp = &val2; *sp = 0;
, it would be a violation of aliasing rules, and the behavior would be undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With