Reader,
Could anyone explain to me what will happen in my computer when I run this piece of false code.
Compiled with the gnu gcc compiler. in Codeblocks.
This is false code:
char data[5];
data[0] = '1';
data[1] = '10';
data[2] = '30';
data[3] = '50';
if(sizeof(data) == 5)
{
adjust(data);
}
sizeof(data) is 5 because I declared char data[5].
If I try to read data[1] I noticed it will return the last char. Either ‘0’ or ’48’.
So I was wondering, what happens with the ‘1’ in data[1] and what will happen to my memory?
It’s a bit confusing that you’re using multi-character literals like
10, that probably adds to your confusion. What will happen with a line like this:is:
int-type (notchar, in C) value'10'will be truncated down tochardata[1].Which value this is, exactly, is compiler-dependent since the literal is larger than what fits in a single
char.If you’re seeing
0(numerically 48 on ASCII systems), this means that10was truncated to0, which is the value stored. The1was then completely lost (not stored in an adjacent slot of the array, which you might have expected).