Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Strange effect with Nulltermination char inside of string

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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…

>Solution :

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";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading