Simple Code example: Godbolt-Compiler Explorer
#include <cstdint>
#include <iostream>
using namespace std;
using u16 = uint16_t;
int main(){
//char str[] = "0123456789\0abcdefghijklmnopqrtsuvwxyz";
char str[] = "0123456789\00123456789\00123456789";
auto str_len = (sizeof(str)-1);
cout << "stringlength: " << str_len << "\n";
for(int ii=0; ii<str_len; ii){
cout << u16(str[ii]) << ",";
}
cout << "\n";
return 0;
}
Output is:
stringlength: 28 48,49,50,51,52,53,54,55,56,57,1,50,51,52,53,54,55,56,57,1,50,51,52,53,54,55,56,57,
which is the same array content as when viewed with a debugger...
Here Termchar, 0 and 1 replaced with a single char with value '1'
but when I change for the other definition of char str[] (the commented out one...)
I get 48,49,50,51,52,53,54,55,56,57,0,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,116,115,117,118,119,120,121,122,
which is correct.
So did I hit a strange unicode thing, or how to explain that effect?
PS: I use that char xyz[] definition, so (sizeof()-1) can be used as compile time strlen() calculation...
CodePudding user response:
The easiest way to stop this from being interpreted as an octal escape sequence would be:
char str[] = "0123456789\0" "0123456789\0" "0123456789";
Or just use \000
as the null character:
char str[] = "0123456789\0000123456789\0000123456789";
CodePudding user response:
One solution - but with an ugly syntax is:
#include <cstdint>
#include <iostream>
using namespace std;
using u16 = uint16_t;
int main(){
char str[] = "0123456789\0" "0123456789\0" "0123456789";
auto str_len = (sizeof(str)-1);
cout << "stringlength: " << str_len << "\n";
for(int ii=0; ii<str_len; ii){
cout << u16(str[ii]) << ",";
}
cout << "\n";
return 0;
}