Question:
I did not think that I would ask such a question, but when reworking the code from Pascal
to C
, strangeness turned out. Pascal
:
for j := 1 to Length(tmp)-1 do begin
and C
for(j = 1; j <= strlen(tmp)-1; j ++) {
This turns out to be different things when Length(tmp)=0
…
Answer:
It turns out that things behave differently in the debugger and in reality. I racked my brains, I realized, in reality: (strlen(tmp) - 1)
not -1
)) Because strlen
returns size_t
(I did not find an implementation of this type), and this is unsigned
, then C somehow works with (strlen(tmp) - 1)
as with unsigned
and, accordingly, the result will be макс_ПОЛОЖИТЕЛЬНОЕ_значение_size_t - 1
… And the debugger considered that (strlen(tmp) - 1)
of type signed
and displayed -1 as a result, and moreover, in the debugger j <= (strlen(tmp) - 1) == 0
but in reality j <= (strlen(tmp) - 1) == 1
for the mentioned reason… This is how "fun" programming is in C)
By the way, this is fixed like this: for(j = 1; j <= (signed)(strlen(tmp) - 1); j ++)