I'll have to sleep on that one.
I'll have to sleep on that one.
What's happening with your original code is that you are using the variable i to iterate through your string, so i goes 0,1,2,3,4,5,6,7,8,9,10,11,12,... until it hits the 0 byte of the null terminated string at s[9]. The problem is, however, that you were also using i to store the temporary value when you checked the modulus: i = s[i] - '0';, so you are clobbering i with this.
Finally, I have reached enlightenment.
Thanks a million.
This is just strange for me, cause just yesterday I had to zero out all my array elements in my loop to make my program work, now it appears in this program, the array is automatically zeroed out.
Sent from my iPadŽ
No, UB is UB. Unitialized data is just that. Sometimes it may be zero, sometimes it may not.Originally Posted by SlyMaelstrom
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
As I said above, when you declare an automatic variable, uninitialised, the value could be anything, all zeroes, the fibonacci sequence, etc.
In reality, when you first start a program on some systems, the stack starts off with zeroes, but once you call a few functions, the values are left over from previous calls, because the stack is never "re-initialised", so in a simple example with a single first function call, you might be "lucky" and get all zeroes, but then when you put another function call before it, the garbage from that call is left on the stack ready for the next function.
Never, EVER rely on undefined behaviour. In fact, don't even bother to see what actually happens, because you're gaining useless and dangerous knowledge. It doesn't cost much to initialise:will initialise the whole array to zeroes.Code:char s[256] = { 0 };
Ok, that works for me.
Sent from my iPadŽ