Talk:c/string/multibyte/mbrtoc32
[edit] Example
Please forgive me for not seeing the obvious, but how does the while loop in the example terminate? Inserting the statement printf("*%d,%ld*\n", rc,end-ptr); as the first statement in the body of the while loop exposes the value of end-ptr that went into the function mbrtoc32() and the return code rc that came from it. At the start of the "final" iteration of the loop, both pointers ptr and end target the same null terminating character in the original string. So, the third argument in the function call is zero, and the return code rc is -2, causing an infinite loop. On the other hand, adding one to the expression str + strlen(str) seems to allow mbrtoc32() to see the null character, return zero, and the modified example avoids the infinite loop. Newatthis (talk) 07:36, 15 January 2017 (PST)
- looks like you're right, the loop was assuming the terminating zero is part of the string (being a port of the C++ example), and so it needed strlen + 1. --Cubbi (talk) 09:17, 15 January 2017 (PST)
[edit] state initialization
In the given code example, it took me a while to realize that state is implicitly 0-initialized because it is a global variable. I think it'd be better to make it a local variable and initialize it explicitly, unless I'm missing something ?